简体   繁体   中英

Modifying Array.prototype causes errors in other js library

I'm using Crystal Reports in ASP.NET webforms to show some report files. The framework generates some javascript that which is used for the UI logic.

Un-minified version (using a beautifier): http://pastebin.com/uryjRZF7

The thing that bothers me is that as soon as I do any modification to the array prototype, the script above throws 2 errors. It looks like this in firebug (yes, I know, but I can't un-minify it at this point):

TypeError: E[D].getHTML is not a function

...conWidget("iconMenu_icon_"+C,B,IconMenuWidget_iconClickCB,K,F,J,G,P,L,A,N,false)...

TypeError: A.layer is null

...conWidget("iconMenu_icon_"+C,B,IconMenuWidget_iconClickCB,K,F,J,G,P,L,A,N,false)...

For example, this would make the errors appear:

if(!Array.prototype.somethingVeryUnique) {

    Array.prototype.somethingVeryUnique = function () {

        return this.length;
    };
}

How can this possibly interfer with the auto generated file?!

Update:

Object.defineProperty(Array.prototype, "somethingUnique", {
    enumerable: false,
    writable: true,
    value: function () {

    }
});

If I make it non-enumerable, it works. However, object.defineProperty doesn't work in IE7, which I need to support.

Is there any way to make a non-enumerable property without it?

They are probably using for(something in array) and the prototype added member will show up if you don't check array.hasOwnProperty(something); Best is to not extend native JS objects .

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