Javascript 向对象原型添加可枚举函数时出现未捕获的TypeError
每当我向对象的原型中添加可枚举函数时,就会出现一些类型错误Javascript 向对象原型添加可枚举函数时出现未捕获的TypeError,javascript,function,prototype,typeerror,enumerable,Javascript,Function,Prototype,Typeerror,Enumerable,每当我向对象的原型中添加可枚举函数时,就会出现一些类型错误 jquery-1.10.2.js:2451 Uncaught TypeError: matchExpr[type].exec is not a function at tokenize (jquery-1.10.2.js:2451) at Function.Sizzle [as find] (jquery-1.10.2.js:1269) at init.find (jquery-1.10.2.js:5744)
jquery-1.10.2.js:2451 Uncaught TypeError: matchExpr[type].exec is not a function
at tokenize (jquery-1.10.2.js:2451)
at Function.Sizzle [as find] (jquery-1.10.2.js:1269)
at init.find (jquery-1.10.2.js:5744)
at change-project-controller.js:4
at change-project-controller.js:255
tokenize @ jquery-1.10.2.js:2451
Sizzle @ jquery-1.10.2.js:1269
find @ jquery-1.10.2.js:5744
(anonymous) @ change-project-controller.js:4
(anonymous) @ change-project-controller.js:255
jquery-1.10.2.js:2451 Uncaught TypeError: matchExpr[type].exec is not a function
at tokenize (jquery-1.10.2.js:2451)
at Function.Sizzle [as find] (jquery-1.10.2.js:1269)
at init.find (jquery-1.10.2.js:5744)
at filter-by-registrant-controller.js:10
at filter-by-registrant-controller.js:179
tokenize @ jquery-1.10.2.js:2451
Sizzle @ jquery-1.10.2.js:1269
find @ jquery-1.10.2.js:5744
(anonymous) @ filter-by-registrant-controller.js:10
(anonymous) @ filter-by-registrant-controller.js:179
jquery-1.10.2.js:2451 Uncaught TypeError: matchExpr[type].exec is not a function
at tokenize (jquery-1.10.2.js:2451)
at Function.Sizzle [as find] (jquery-1.10.2.js:1269)
at init.find (jquery-1.10.2.js:5744)
at registrations-controller.js:6
at registrations-controller.js:412
tokenize @ jquery-1.10.2.js:2451
Sizzle @ jquery-1.10.2.js:1269
find @ jquery-1.10.2.js:5744
(anonymous) @ registrations-controller.js:6
(anonymous) @ registrations-controller.js:412
Index:290 Uncaught TypeError: Cannot read property 'registerFilter' of undefined
at Index:290
(anonymous) @ Index:290
请注意,四个错误中的最后一个与jQuery无关
这是导致错误发生的代码:
Object.defineProperty(Object.prototype, "select", {
enumerable: true,
value: function () {
return "hello world";
}
});
如果将函数添加为不可枚举,则不会出现错误,如下所示:
Object.defineProperty(Object.prototype, "select", {
enumerable: false,
value: function () {
return "hello world";
}
});
请注意,唯一的区别是可枚举成员设置为false
。另外,如果我更改要添加到数组而不是对象的可枚举函数,代码运行良好
我正在处理的项目不是我的,因此我无法共享它,而且我还没有成功地在JSFIDLE或简单的HTML文件中复制错误
每当我向对象的原型中添加可枚举函数时,就会出现一些类型错误
jquery-1.10.2.js:2451 Uncaught TypeError: matchExpr[type].exec is not a function
at tokenize (jquery-1.10.2.js:2451)
at Function.Sizzle [as find] (jquery-1.10.2.js:1269)
at init.find (jquery-1.10.2.js:5744)
at change-project-controller.js:4
at change-project-controller.js:255
tokenize @ jquery-1.10.2.js:2451
Sizzle @ jquery-1.10.2.js:1269
find @ jquery-1.10.2.js:5744
(anonymous) @ change-project-controller.js:4
(anonymous) @ change-project-controller.js:255
jquery-1.10.2.js:2451 Uncaught TypeError: matchExpr[type].exec is not a function
at tokenize (jquery-1.10.2.js:2451)
at Function.Sizzle [as find] (jquery-1.10.2.js:1269)
at init.find (jquery-1.10.2.js:5744)
at filter-by-registrant-controller.js:10
at filter-by-registrant-controller.js:179
tokenize @ jquery-1.10.2.js:2451
Sizzle @ jquery-1.10.2.js:1269
find @ jquery-1.10.2.js:5744
(anonymous) @ filter-by-registrant-controller.js:10
(anonymous) @ filter-by-registrant-controller.js:179
jquery-1.10.2.js:2451 Uncaught TypeError: matchExpr[type].exec is not a function
at tokenize (jquery-1.10.2.js:2451)
at Function.Sizzle [as find] (jquery-1.10.2.js:1269)
at init.find (jquery-1.10.2.js:5744)
at registrations-controller.js:6
at registrations-controller.js:412
tokenize @ jquery-1.10.2.js:2451
Sizzle @ jquery-1.10.2.js:1269
find @ jquery-1.10.2.js:5744
(anonymous) @ registrations-controller.js:6
(anonymous) @ registrations-controller.js:412
Index:290 Uncaught TypeError: Cannot read property 'registerFilter' of undefined
at Index:290
(anonymous) @ Index:290
不要这样做。正如您所发现的,这样做会破坏许多毫无戒心的代码。默认情况下,空白对象没有可枚举属性。例如:
var o={};
for(o中的变量名称){
log(“这一行永远不会在合理的世界中运行。”);
}
控制台日志(“结束”)我明白你的意思,我完全尊重你的意思。然而,我很困惑为什么我的registerFilter函数会突然消失,因为我添加了一个可枚举函数。有什么想法吗?@robinthilade:我恐怕想不出来,我们必须有一个MCVE,你说你没能创造出来。从根本上说,某个地方的某些东西被上面的问题绊倒了,这会产生连锁反应,导致您看到的错误。唯一确定的方法是继续生产MCVE(在生产过程中,您可能会发现)。但这不太可能是一种有用的消磨时间的方式;基本上,只需不向根原型添加可枚举项即可。:-)