prototype-chain

How does __proto__ work when object is created with Object.create(null)

喜你入骨 提交于 2021-02-04 17:36:06
问题 Consider the following javascript code var a = Object.create(null); a.foo = 1; var b = Object.create(a); console.log(b.foo); //prints 1 console.log(b.__proto__); //prints undefined b.__proto__ = null; console.log(b.__proto__); //prints null console.log(b.foo); //prints 1 Can anyone explain how object b is accessing the "foo" property of a even after setting b.__proto__ to null? What is the internal link which is used to access the property of a ? I tried searching through SO for possible

How to iterate over an object's prototype's properties

旧城冷巷雨未停 提交于 2019-12-23 09:35:10
问题 I have some code: var obj = function() { }; // functional object obj.foo = 'foo'; obj.prototype.bar = 'bar'; for (var prop in obj) { console.log(prop); } What surprised me is that all that is logged is foo . I expected the for loop to iterate over the properties of the obj 's prototype as well (namely bar ), because I did not check for hasOwnProperty . What am I missing here? And is there an idiomatic way to iterate over all the properties in the prototype as well? I tested this in Chrome and

Use Array Prototype Functions with non-arrays

别等时光非礼了梦想. 提交于 2019-12-11 10:37:22
问题 As we know we can use Array prototype methods with function arguments just like below. function abc() { // As 'arguments' is a array like object which don't have Array prototype functions. Array.prototype.push.call(arguments, 30); console.log(arguments); } abc(10, 20); // Output is: [10, 20, 30] So like wise I tried to use push on DOM element classList like below which gives me an error "Cannot set property length of [object Object] which has only a getter". var bodyClassList = document.body

How function resolution in prototype chain will work for Object.prototype as constructor

巧了我就是萌 提交于 2019-12-10 22:51:32
问题 I am referring to this article on Helephant.com, to learn how Javascript resolves functions in the prototype chain when called on objects. The article quotes, If the object doesn’t have the method set directly on it, javascript then looks for a Constructor function that created the object. Javascript checks the constructor’s prototype property for the method. In the following code, if you check rufus.constructor is the global Object() constructor, so will JS directly check the global Object()

How to make a JavaScript object's prototype permanent?

回眸只為那壹抹淺笑 提交于 2019-12-10 15:16:11
问题 Can I enforce that the prototype of an object not be changed? Note! There are some requirements: The object should behave just like a regular object literal (add/remove/configure/modify properties and descriptors on the object), with literally the only new limitation being that the prototype is permanent. So other than the prototype being permanent, I don't want to add any other limitations (tools like Object.seal/freeze/preventExtensions impose more limitations on the object). Would I have

Why assign Something to Something.prototype.constructor?

£可爱£侵袭症+ 提交于 2019-12-06 01:41:43
问题 I was reading about how the Javascript prototype property works along with inheritance and then began to look through the Angular.js code and came up with some questions. First off, I read that the prototype property points to an object that has a "constructor" property which points back to the original function that is used to create the object. So for example: // This is the constructor function Shape() { this.position = 1; } // The constructor points back to the original function we

Why assign Something to Something.prototype.constructor?

假装没事ソ 提交于 2019-12-04 06:28:51
I was reading about how the Javascript prototype property works along with inheritance and then began to look through the Angular.js code and came up with some questions. First off, I read that the prototype property points to an object that has a "constructor" property which points back to the original function that is used to create the object. So for example: // This is the constructor function Shape() { this.position = 1; } // The constructor points back to the original function we defined Shape.protoype.constructor == Shape; The prototype also contains any other methods or properties that

javascript: find the prototype object to which a property belongs

二次信任 提交于 2019-12-01 09:13:23
I have an instance from Square which inherits from Rectangle instance instanceof Rectangle --> true instance instanceof Square --> true instance.area() ; // --> area is defined by Rectangle Now, in my code I don't know where the 'area' function is defined and I want the prototype object which defines it. Of course I can traverse the prototype chain (not tested) var proto = instance ; while( !(proto = Object.getPrototypeOf(proto)).hasOwnProperty('area') ) {} // do something with 'proto' However, I was wondering if there is a better/faster way to get the prototype object to which a function

javascript: find the prototype object to which a property belongs

爱⌒轻易说出口 提交于 2019-11-30 17:26:45
问题 I have an instance from Square which inherits from Rectangle instance instanceof Rectangle --> true instance instanceof Square --> true instance.area() ; // --> area is defined by Rectangle Now, in my code I don't know where the 'area' function is defined and I want the prototype object which defines it. Of course I can traverse the prototype chain (not tested) var proto = instance ; while( !(proto = Object.getPrototypeOf(proto)).hasOwnProperty('area') ) {} // do something with 'proto'

Prototype chain: call “super” method over multiple levels

帅比萌擦擦* 提交于 2019-11-28 14:23:53
I have got the following prototype chain SuperSuperClass SuperClass Class each with a method named do . What is the common approach for calling the respective super class method? For the moment I use <ClassName>.prototype.__proto__.<methodName>.call(this) but that looks odd. Using the following code the console prints (as expected): Class.prototype.do SuperClass.prototype.do SuperSuperClass.prototype.do SuperSuperClass = function SuperSuperClass() {} SuperSuperClass.prototype.do = function() { console.log('SuperSuperClass.prototype.do'); }; function SuperClass() { SuperSuperClass.call(this); }