Prototypal inheritance without new or Object.create

Is there any way to include an object to inherit from the prototype (1) without use newand (2) without use Object.create. (I had a suspicion that a new one was present in the method Object.create, but after the Firebug console told me its own code, well, I have a suspicion that it is native.) When I say "inherit from prototype", I have in mind real prototypical inheritance of JavaScript (i.e. not just imitation). I know the real inheritance when I see this (i.e. the Property is __proto__present, there is a circular link between the constructor and the prototype, there is an inheritance hierarchy).

My question comes down to: although "we are barely new ya," are the only two inheritance mechanisms newand Object.create?

+5
source share
4 answers

[...] - the only two mechanisms for inheritance newand Object.create?

Yes they are. At least these are the ones you should use.

You can directly assign __proto__, but this is not a standard property. Additional Information: MDN -__proto__ .

+2
source

As you know, newand Object.createperform a hidden action, what we can call setPrototype.

Object.create = function(proto, properties) {
    var obj = {};
    setPrototype(obj, proto);
    Object.defineProperties(obj, properties);
    return obj;
}

function fakeNew(Constructor) {
    var obj = {};
    setPrototype(obj, Constructor.prototype);
    Constructor.call(obj);
    return obj;
}

, "new Object.create" "Object.create ". .

setPrototype, , , , .

function setPrototype(obj, proto) {
    obj.__proto__ = proto;
}
+1

new, , ( SomeClass), ( new ):

function SomeClassFacade() {
  return new SomeClass();
}

SomeClass.prototype = {
 /* ... */
};
0

ES6 Reflect.construct, new:

function Constructor() {}
var instance = Reflect.construct(Constructor, []);
Object.getPrototypeOf(instance); // Constructor.prototype

- Object.create, ES6 , null:

var obj = (class extends null{}).prototype;
delete obj.constructor;
Object.getPrototypeOf(obj); // null
function Constructor() {}
var obj = (class extends Constructor{}).prototype;
delete obj.constructor;
Object.getPrototypeOf(obj); // Constructor.prototype
0

All Articles