Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
JavaScript Prototype Chain Reference to Object.prototype

I need some help for understanding the prototype chain. I don't understand / not quite sure if my assumptions are correct. I want to understand the link to the Object.prototype. One example

function A(){};
var newObject=new A();

I know the newObject has an internal property [[prototype]] which has a reference to A.prototype. And this prototype-Object has also an internal property with reference to Object.prototype, right? But why does it have that reference? Is it because the prototype of A (used as constructor) is an object which can be imagined to be created by A.prototype=new Object() ( which will be done automatically in the background). And now I have a reference to the prototype of Object? I hope I explained it clearly. Please let me know your comments.

Many thanks

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Yes, your understanding is correct.

In JS, pretty much all the objects have a reference to Object.prototype in their prototype chain.

The [[Prototype]] of an object refers to Object.prototype as the end of a prototype chain. In your example, this means what you described:

            [[Prototype]]                  [[Prototype]]                   
newObject -----------------> A.prototype -----------------> Object.prototype

That applies to...

  • objects created by the Object constructor (i.e. new Object(...)),
  • those created with an object literal (which you can think of as syntax sugar for a new Object() call followed by a bunch of Object.defineProperty calls) and
  • objects created internally by the JS interpreter (like A.prototype which is created automatically when you define the A function).

There are also exceptions of this "rule" though:

  • Object.prototype itself.

    If it had a reference to... well, itself, it would create and infinite recursion when a nonexistent property is looked up. Hence, it has no prototype, which means that its [[Prototype]] is null (that's how a prototype chain "ends").

  • Custom objects created using Object.create or modified with Object.setPrototype.

    The [[Prototype]] can be set to anything this way, including null or other objects whose prototype chain doesn't end with Object.prototype.

about 4 years ago · Juan Pablo Isaza Relatório

0

This is indeed what the ECMAScript specification prescribes. When a function is defined with the function keyword, then an object is created from Object.prototype which is assigned to the function's prototype property, and therefor will be used as prototype for any object that is constructed by that function.

This specification can be found at Make Constructor, step 5.a:

5.a. Set prototype to OrdinaryObjectCreate(%Object.prototype%).

about 4 years ago · Juan Pablo Isaza Relatório

0

When it comes to javascript every value is either an object (arrays, objects, sets, maps...) or a primitive(Numbers, strings, bool ...) objects and primitives are the two data types in javascript a . You have the Number, String , Array and even Object constructors which you can use to create the corresponding value. And when you create new object using the new operator the following steps happen consecutively:

// 1. New empty object is created

// 2. the constructor function is called, and the this keyword is assigned to the empty object i.e this = {}.

// 3. the empty object is linked to the prototype of the constructor functio i.e {}.proto = constructorFunction.prototype.

// 4. the constructor function automatically return the new object {}.

keep in mind that constructorFunction.prototype is the prototype of the new object that is created from the constructorFunction and not of itself.

I hope this clears things up a little for you!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda