__proto__ deprecated. What is fast and cross browser alternative?

684 views Asked by At

As you know, __proto__ is deprecated.

MDN: Warning: The __proto__ property is deprecated and should not be used.

what is fast and cross browser alternative for __proto__? In my case I change __proto__ of a function. So I can't find any alternative.

deprecated code

// one time
var proto = {};
proto.bar = function (x) { return this(x) + 10; };
proto.buzz = function () { return this(4); };
// and 100 more function proto.*
proto = _.extend(proto, (function(){}).__proto__); // underscore extend function
                                                   // update: change arguments order

// many time
var foo = function (x) { return 2 * x; }
foo.__proto__ = proto;

slow alternative (jsperf)

// many time
var foo = function (x) { return 2 * x; };
foo.bar = function (x) { return this(x) + 10; };
foo.buzz = function () { return this(4); };
// and 100 more function foo.*

Is there any fast and cross browser alternative?

2

There are 2 answers

3
Scimonster On

As it says in the linked MDN article, Object.getPrototypeOf() is the new function. It seems to have pretty good browser support. For those that don't support it (except IE), you can shim it with __proto__. ;)

if (!Object.getProtoTypeOf) {
    Object.getProtoTypeOf = function(obj) {
        return obj.__proto__;
    };
}
10
six fingered man On

In your first use, you're getting the __proto__ of a function, so you can simply use Function.prototype

proto = _.extend(Function.prototype, proto); // underscore extend function

Your second use is replacing the prototype on an object, so there's no alternative.

ECMAScript 6 is standardizing __proto__, and will also have Object.setPrototypeOf(), so you'll be able to use them once implemented.