My expectation from the following code would be that if I checked a.name, it would search the prototype and return it as it was declared. Can anyone pinpoint what it is that is preventing JS from acknowledging my prototype?
var obj = function(parent){
return {
prototype: parent
}
};
var me = { name: 'keith' };
var a = new obj(me)
// => undefined
a.name
// => undefined
a.prototype.name
// => "keith"
A property named “prototype” is just a property, it does not point to the object from which the object inherits. Use
Object.getPrototypeOfor the non-standard__proto__property to get that.So what your function
obj(me)returns is just an object with a property “prototype” which points to an object with a property “name” which points to the stringkeith. As your function returns an object, it makes no difference whether it is called with thenewkeyword or not.For inheritance, the “prototype” property of the constructor function [object] is of concern. Every object created by this constructor (which does not return an object) with the
newkeyword inherits from the object to which the “prototype” property of the constructor points. So you could do this: