Menu
  • HOME
  • TAGS

set an object's prototype using object.create

javascript,prototype,prototype-chain

Two issues: The first parentFunction you define is in the constructor for Parent, not the prototype. So Parent.prototype.parentFunction isn't defined. Instead, there is a separate copy of the parentFunction for ever instance of Parent. In the Child constructor, this.constructor.prototype refers to the prototype of Child, not the prototype of Parent....

Inheritance within object literals

javascript,oop,inheritance,javascript-objects,prototype-chain

It is posible to inherit proprietyes and methods within object literals? Not yet, but with ES6, this will be possible: var foo = { __proto__: bar }; where bar becomes the prototype of foo. However, I rather think you mean whether it's possible to create objects with a specific...

JavaScript iterating over object properties and the prototype chain

javascript,inheritance,prototype,prototypal-inheritance,prototype-chain

Because Object.prototype.hasOwnProperty is non-enumerable: Object.getOwnPropertyDescriptor(Object.prototype, 'hasOwnProperty') .enumerable // false Therefore, it's not iterated by the for...in loop....

how do i create an Object.prototype clone

javascript,object,prototype,prototype-chain,prototype-oriented

I don't understand very much what you are attempting to do, but you can clone Object.prototype like this: var proto = Object.prototype, clone = Object.create(null), props = Object.getOwnPropertyNames(proto); for(var i=0; i<props.length; ++i) Object.defineProperty(clone, props[i], Object.getOwnPropertyDescriptor(proto, props[i]) ); Object.freeze(clone); ...

Why assign Something to Something.prototype.constructor?

javascript,angularjs,prototype-chain

By default, every prototype has a constructor property that refers to the function that it "belongs" to. function A() { } console.log(A.prototype.constructor === A); // true If you overwrite a prototype in its entirety with an object literal or with some other constructed prototype, this constructor value will get wiped...

Why is mutating the [[prototype]] of an object bad for performance?

javascript,performance,prototype,prototype-chain

// This is bad: //foo.__proto__.bar = bar; // But this is okay Foo.prototype.bar = bar; No. Both are doing the same thing (as foo.__proto__ === Foo.prototype), and both are fine. They're just creating a bar property on the Object.getPrototypeOf(foo) object. What the statement refers to is assigning to the...

Why can't I view __proto__ upon object creation?

javascript,object,google-chrome-devtools,prototype-chain

Who knows? It appears to be a design decision on the part of the Chrome debugger's implementers. Unless someone here is privy to their decision process, I think this question is off topic. Perhaps they figured that you didn't need to be able to expand objects unless they have methods....