Create a reset of javascript Array prototype when Array.prototype has been modified?

后端 未结 2 793
情深已故
情深已故 2020-11-30 09:09

General question: When a default Javascript prototype like Array has been modified, hacked, changed and twisted to the point of being unusable, is there any

2条回答
  •  鱼传尺愫
    2020-11-30 09:38

    You could just copy Array methods from the iframe:

    Array.prototype.slice = function() {
        return "trololol";
    };
    var a = document.createElement("iframe");
    a.src = "about:blank";
    document.body.appendChild(a);
    var prototype = a.contentWindow.Array.prototype;
    var fn = ["toString", "toLocaleString", "join", "pop", "push", "concat", "reverse", "shift", "unshift", "slice", "splice", "sort", "filter", "forEach", "some", "every", "map", "indexOf", "lastIndexOf", "reduce", "reduceRight"];
    for (var i = 0; i < fn.length; ++i) {
        var methodName = fn[i];
        var method = prototype[methodName];
        if (method) {
            Array.prototype[methodName] = method;
        }
    }
    document.body.removeChild(a);
    

    ​ Here's the jsfiddle that works in chrome and IE9, don't have time to figure out IE7-8. http://jsfiddle.net/jMUur/1/

    To check if an object is an Array without relying on references:

    function isArray( obj ) {
         return {}.toString.call( obj ) === "[object Array]";
    }
    

提交回复
热议问题