最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - __proto__ for IE9 or IE10 - Stack Overflow

programmeradmin8浏览0评论

Is there any possibility to change the __proto__ property of an object in IE9 or IE10? Or is MS still not planning to include it in their JS engine?

I need it in a very special situation where I need to change __proto__ after the object is created.

Is there any possibility to change the __proto__ property of an object in IE9 or IE10? Or is MS still not planning to include it in their JS engine?

I need it in a very special situation where I need to change __proto__ after the object is created.

Share Improve this question edited Aug 4, 2014 at 7:15 rene 42.5k78 gold badges121 silver badges165 bronze badges asked Dec 7, 2011 at 10:04 Van CodingVan Coding 24.6k25 gold badges92 silver badges137 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 12

__proto__ is going to be standardized in ES6. It is currently in Appendix B of the ES6 draft which in practice means that if it is implemented it needs to have the following semantics.

__proto__ is both available as an accessor on Object.prototype which means that all objects can read and write it by default. However, it can be removed from Object.prototype (using delete). Once deleted __proto__ will work as a normal data property with no side effects on setting.

__proto__ is also a special syntactic form in object literals. It will work to set the [[Prototype]] even if Object.prototype.__proto__ was deleted.

var p = {a: 1};
var o = {
  __proto__: p,
  b: 2
}

ES6 also introduces Object.setPrototypeOf (not in the appendix). This is preferred over setting __proto__.

__proto__ is available in all modern browsers, including Internet Explorer 11.

__proto__ is included in IE11 found in the leaked build of Windows Blue: http://fremypany./BG/2013/Internet-Explorer-11-rsquo-s-leaked-build-395/

A nonanswer as a last case resort:

Change your code so that all the properties that would originally be accessed via the changed prototype are now accessed via explicit delegation over a normal property:

{
   a: 17,
   __proto__: { ... }
}

to

{
   a: 17,
   proto: {...}
}

I'm not sure what exactly it is you're after since your question didn't specify, but for most uses of proto, you should be able to use prototype.

var foo = new Bar();
//this also affects foo
Bar.prototype.baz = something;

IE 9 and 10 do not support setting the prototype of an object that has already been created, but there is a hack that works in IE 9 and 10:

// An alternative to Object.setPrototypeOf() for IE 9 and 10
function addPrototypeTo(obj, prototype) {
    if (!/c/.test(typeof prototype)) {// /c/ matches function and object
        // Throws if prototype is neither an object nor null
        throw new TypeError;
    }
    if (/c|d/.test(typeof obj) && prototype) {// /c|d/ matches function, object, and undefined
        for (var prop, keys = Object.getOwnPropertyNames(prototype), length = keys.length, i = 0; i<length; i++) {
            if (!{}.hasOwnProperty.call(obj, prop = keys[i])) {// Throws if obj is nullish
                Object.defineProperty(obj, prop, Object.getOwnPropertyDescriptor(prototype, prop));
            }
        }
    }
    return obj;
}

The above function is a hack that simply copies all the properties from a prototype object and defines them on another object without overwriting any conflicting property names. Since it uses Object.getOwnPropertyDescriptor() and Object.defineProperty(), getters and setters and other property descriptors such as enumerability are maintained.

Now if you wanted to polyfill Object.setPrototypeOf() you could simply do this:

if (document.documentMode>8) {// IE 9 and above
   Object.setPrototypeOf = Object.setPrototypeOf || addPrototypeTo;
}

However this hack obviously does not actually change the prototype of an object. And changes you make to the prototype after calling the function will have no effect on the object. So this may not suit your needs.

发布评论

评论列表(0)

  1. 暂无评论