简体   繁体   中英

Uncaught TypeError when adding enumerable function to Object prototype

I get a few type errors whenever I add an enumerable function to the prototype of Object.

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

Notice that the last of the four errors does not have anything to do with jQuery.

This is the code that causes the error to occur:

Object.defineProperty(Object.prototype, "select", {

    enumerable: true,
    value: function () {

        return "hello world";

    }

});

I do not get the errors if I add the function as non-enumerable, like this:

Object.defineProperty(Object.prototype, "select", {

    enumerable: false,
    value: function () {

        return "hello world";

    }

});

Notice that the only difference is the enumerable member is set to false . Also if I change the enumerable function to be added to Array rather than Object the code runs fine.

The project I'm working on is not mine so I cannot share it and I have not been successful at reproducing the error on jsfiddle or in a simple HTML file.

I get a few type errors whenever I add an enumerable function to the prototype of Object.

Don't do that. As you've found, doing that will break a lot of unsuspecting code. The default state of things is that a blank object has no enumerable properties. Eg:

 var o = {}; for (var name in o) { console.log("This line never runs in a reasonable world."); } console.log("End"); 

By adding an enumerable property to Object.prototype , you break that:

 Object.prototype.foo = function() { }; var o = {}; for (var name in o) { console.log("I wasn't expecting to find: " + name); } console.log("End"); 

Adding things to Object.prototype is almost never a good idea. Adding enumerable things to it is always a Bad Idea™. All modern browsers support defineProperty , so if you must augment Object.prototype , do so with non-enumerable properties. (Note, though, that it's easy to introduce incompatibilities even with non-enumerable Object.prototype properties.) If you need to support obsolete browsers that don't support it, you need to leave Object.prototype alone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM