__proto__ for IE9 or IE10
Asked Answered
C

5

6

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.

Companionable answered 7/12, 2011 at 10:4 Comment(0)
L
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.

Librium answered 10/8, 2012 at 21:7 Comment(3)
I hope this is true. Do you know if ES6 will allow to derive efficiently from Array?Softy
proto is not on a standards track - instead ES6 approved Object.setPrototypeOf for better support developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…Husein
Yes ES6 will support subclassing built in native classes like Array.Dierolf
J
2

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

Joannajoanne answered 4/4, 2013 at 22:47 Comment(0)
F
1

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: {...}
}
Friedrich answered 7/12, 2011 at 12:37 Comment(0)
J
0

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;
Jin answered 7/12, 2011 at 10:31 Comment(2)
I didn´t specify, because the only thing I want to know is if IE9/10 supports modifying the proto property, since it´s the only browser that does not support it at the moment.Companionable
I don't understand "new Bar();". How can you rewrite this using Object.create?Gurley
C
0

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.

Crabtree answered 2/2 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.