Javascript Object.prototype不支持选项[method name]
我正在用Javascript Object.prototype不支持选项[method name],javascript,node.js,Javascript,Node.js,我正在用Object.prototype扩展我的webapp中的Object类,如下所示,但后端返回错误,表示不支持选项[allFalse] let MongoClient = require('mongodb').MongoClient; Object.prototype.allFalse = function() { for (var i in this) { if (i < 1) return; if (this[i] === true) return false;
Object.prototype
扩展我的webapp中的Object类,如下所示,但后端返回错误,表示不支持选项[allFalse]
let MongoClient = require('mongodb').MongoClient;
Object.prototype.allFalse = function() {
for (var i in this) {
if (i < 1) return;
if (this[i] === true) return false;
}
return true;
}
router.get('/getmongo', function(req, res) {
MongoClient.connect(process.env.DB_CONN, function(err, db) {
(bunch of codes here)
}
}
让MongoClient=require('mongodb')。MongoClient;
Object.prototype.allFalse=函数(){
for(本节中的var i){
如果(i<1)返回;
如果(此[i]==true)返回false;
}
返回true;
}
router.get('/getmongo',函数(req,res){
MongoClient.connect(process.env.DB_CONN,函数(err,DB){
(这里有一堆代码)
}
}
我用以下代码替换了代码,它工作得非常好
let MongoClient = require('mongodb').MongoClient;
Object.defineProperty(Object.prototype, 'allFalse', {
value : function() {
for (var i in this) {
if (i < 1) return;
if (this[i] === true) return false;
}
return true;
}
});
router.get('/getmongo', function(req, res) {
MongoClient.connect(process.env.DB_CONN, function(err, db) {
(bunch of codes here)
}
}
让MongoClient=require('mongodb')。MongoClient;
Object.defineProperty(Object.prototype,'allFalse'{
值:函数(){
for(本节中的var i){
如果(i<1)返回;
如果(此[i]==true)返回false;
}
返回true;
}
});
router.get('/getmongo',函数(req,res){
MongoClient.connect(process.env.DB_CONN,函数(err,DB){
(这里有一堆代码)
}
}
有人能向我解释为什么吗
“但是后端返回错误”…“我用以下代码替换了代码,它工作得非常好。”
不同之处在于,第一个变量使allFalse
属性可枚举。因此,在将选项发送到后端之前以某种方式序列化的代码也会序列化allFalse属性
考虑下面的代码片段,它伪造了可能的序列化,而在第一种情况下,扩展对象原型的方式会影响序列化
Object.prototype.allFalse1=函数allFalse1(){}
Object.defineProperty(Object.prototype,“allFalse2”{
值:函数allFalse2(){}
})
常数a={a:1}
console.log(序列化(a))
console.log(
Object.getOwnPropertyDescriptor(Object.prototype,“allFalse1”)。可枚举,
Object.getOwnPropertyDescriptor(Object.prototype,“allFalse2”)。可枚举
)
//遍历继承属性的伪序列化
函数序列化(obj){
常量参数=[]
用于(obj中的常量键){
push(`${key}=${encodeURIComponent(obj[key])}`)
}
返回参数join(“&”)
}
“但是后端返回错误”…“我用以下代码替换了代码,它工作得非常好。”
不同之处在于,第一个变量使allFalse
属性可枚举。因此,在将选项发送到后端之前以某种方式序列化的代码也会序列化allFalse属性
考虑下面的代码片段,它伪造了可能的序列化,而在第一种情况下,扩展对象原型的方式会影响序列化
Object.prototype.allFalse1=函数allFalse1(){}
Object.defineProperty(Object.prototype,“allFalse2”{
值:函数allFalse2(){}
})
常数a={a:1}
console.log(序列化(a))
console.log(
Object.getOwnPropertyDescriptor(Object.prototype,“allFalse1”)。可枚举,
Object.getOwnPropertyDescriptor(Object.prototype,“allFalse2”)。可枚举
)
//遍历继承属性的伪序列化
函数序列化(obj){
常量参数=[]
用于(obj中的常量键){
push(`${key}=${encodeURIComponent(obj[key])}`)
}
返回参数join(“&”)
}