Questions tagged [prototype-chain]

Almost everything in JavaScript is an object, which you can think of as sort of like associative arrays - objects contain named properties which can be accessed with obj.propName or obj['propName']. Each object has an internal property called prototype, which links to another object. The prototype object has a prototype object of its own, and so on – this is referred to as the prototype chain.

102 questions
59
votes
4 answers

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

From the MDN docs for the standard setPrototypeOf function as well as the non-standard __proto__ property: Mutating the [[Prototype]] of an object, no matter how this is accomplished, is strongly discouraged, because it is very slow and…
basarat
  • 207,493
  • 46
  • 386
  • 462
11
votes
2 answers

Are there efficient ways to use `__proto__` or `setPrototypeOf()` in javascript?

MDN has a huge scary warning about modifying the prototype in your code: Changing the [[Prototype]] of an object is, by the nature of how modern JavaScript engines optimize property accesses, a very slow operation, in every browser and…
B T
  • 46,771
  • 31
  • 164
  • 191
8
votes
3 answers

How `this` works from a Classical Method of Prototyping in Javascript

I'm on my journey to learn Object-Oriented Programming in Javascript. I got this video lession from here http://www.objectplayground.com/ which I understood quite a bit the prototypal method over classical method. While watching the lession, I was…
7
votes
1 answer

How does 'this' keyword work in prototype chain?

Hi experts here is my code and I'm stuck how this keyword is adding property to a object. function carMaker(){ this.companyName='Lamborghini'; } let LamborghiniUrus = new carMaker(); carMaker.prototype.country="Italy" …
Mobeen Sarwar
  • 514
  • 5
  • 22
7
votes
2 answers

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

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…
Priyanjit
  • 280
  • 3
  • 9
6
votes
3 answers

Difference between Function and Function.prototype

According to this, functions inherit from Function and Function from Function.prototype in turn: The global Function object has no methods or properties of its own, however, since it is a function itself it does inherit some methods and properties…
cadaniluk
  • 14,449
  • 2
  • 34
  • 61
6
votes
1 answer

Why can't I view __proto__ upon object creation?

When I create a blank object: var o = {}; Why can't I view the '__proto __' object when I create a new object, but I can when I add a function? Edit: For completeness, to create a truly blank object (no prototypal linkage), we could do: var o =…
Data
  • 1,217
  • 10
  • 16
6
votes
1 answer

Why assign Something to Something.prototype.constructor?

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…
Triad
  • 504
  • 1
  • 5
  • 14
6
votes
2 answers

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

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…
gwg
  • 7,111
  • 8
  • 50
  • 87
6
votes
1 answer

What's the relationship between Number and Function.prototype in javascript?

I'm reading the book Javascript: the Good Parts. I'm a little confused when I read the code below: Function.prototype.method = function (name, func) { this.prototype[name] = func; return this; }; Number.method('integer',function(){ …
ChandlerQ
  • 1,358
  • 2
  • 17
  • 29
5
votes
1 answer

Object class comes twice in prototype chain of DOMWindow?

Why do we have 2 class Object and again Object in prototype chain of window? window --> DOMWindow --->Object --->Object ---> null Can anyone please give me some point about this design? Follwing is the output on chrome.
P K
  • 8,662
  • 12
  • 48
  • 90
5
votes
2 answers

Different ways of creating javascript prototype inheritance chain

function Parent(parentName){ this.name = parentName; } Parent.prototype.printName = function(){ console.log(this.name); } var p = new Parent("Parent"); console.log(p); function Child(parentName, childName){ Parent.call(this, parentName); …
phani
  • 3,123
  • 7
  • 33
  • 62
4
votes
1 answer

Why do square brackets around __proto__ makes it enumerable in an object

When using square brackets around __proto__ in a object, __proto__ is enumerable. When no square brackets are provided, __proto__ is not enumerable. Example: obj = {"__proto__": "Hello"} for (var k in obj) { console.log(k) }; // No…
4
votes
0 answers

How do I use a prototype in a JavaScript factory function?

I've recently found an interest in the factory function pattern in JavaScript. Classes may be pretty clean, but I've found they've got their fair share of issues. (I'm not going to even talk about them because that isn't at all the purpose of this…
Aaron Beaudoin
  • 997
  • 1
  • 7
  • 19
4
votes
2 answers

How to make a JavaScript object's prototype permanent?

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…
trusktr
  • 34,715
  • 41
  • 148
  • 226
1
2 3 4 5 6 7