简体   繁体   中英

Array.prototype is overriden

The problem is my code is working under the assumption that the Array prototype object will not get modified by 1st party, 3rd party, or browser extension code. This is a dangerous assumption that cannot be made in JavaScript and it caused an avoidable CSO. How do I go about bulletproofing the code to prevent it from breaking in this circumstance?

It is the nature of the Javascript. If you want to ensure that something is not overriden you must test expected behavior before using it. Let say you are using Array.prototype.push . Check its behavior, if it is not works like expected throw error.

var arr = [1, 2];
arr.push(3);
if (arr[2] !== 3) {
   throw "Push function is overriden!";
}

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