首页 > 面向对象,扩展内置对象方法为何不行

面向对象,扩展内置对象方法为何不行

面向对象,扩展内置对象方法为何不行

String.prototype={
        constructor:this,
        run:function () {
            alert("success!");
        }
    };

    var n="####";
    n.run();

用这个吧..

String.prototype.run = function () {
            alert("success!");
        };

因为String.prototype是只读&无法修改的

console.log(Object.getOwnPropertyDescriptor(String, 'prototype'));
//Object {value: String, writable: false, enumerable: false, configurable: false}

可以自己模拟这个效果

顺带一提这是ES5特性,IE8不兼容

【热门文章】
【热门文章】