Is the Prototype property of an object, an object?

I've read many awesome articles and discussions (like this one ) about object prototype properties, but there's a big question here. When we observe the behavior of the prototype property , we realize that it is actually a different object. We can see that it has its own proto and constructor properties , just like an object.

var myobj= function(){};
myobj.prototype.constructor== myobj  --> true

      

So these are the questions:

  • Is "prototype" the object itself?
  • What is the relationship between the prototype and the object it refers to (in this example myobj )?
  • Why is myobj.prototype .__ proto __ defaulted to Object {} "but its constructor is myobj ?
  • We know: " __proto__

    is the actual object that is used in the lookup chain to resolve methods, etc. the prototype is the object that is used to build when creating an object with a new one ." But we can see that this is not only what the prototype does. I think: Prototype acts like a container object of all the common methods and properties of the object! Then, when an object is instantiated using a new one , its internal [[Prototype]] points to the prototype where the general behaviors and properties of the class are instantiated! It's true? __proto__

In the end: It seems when we instantiate a class the instance constructor is set to the prototype constructor of that object. Can such a conclusion be drawn?

var b= new myobj();
 b.constructor== myobj.prototype.constructor --> true
 b.constructor == myobj --> true
 myobj.prototype={};
 b.constructor == myobj  --> false
 b.constructor== Object --> true
 myobj.prototype.constructor== Object --> true

      

+3


source to share


2 answers


  • Yes, a prototype object is an object. It is implicitly built along with the function.
  • I would not call it a "parent". Their only relationship is that the function myobj

    has a property .prototype

    with a prototype object as its value.
  • The prototype object inherits from Object.prototype

    . It has its own property .constructor

    with a function myobj

    as its value. ( This is really just a property )
  • Yes it's true. .__proto__

    is a non-standard accessory for the [[Prototype]] inner box. Thus, they are often used synonymously.
  • No, when you instantiate the property is not set .constructor

    . All that happens is a new instance inheriting from the prototype object, and the prototype object has a property constructor

    . Note that myobj.prototype={};

    your example does not affect your previously created instances.


+5


source


I know there is an acceptable answer, but there seems to be some confusion about what a prototype is, what a constructor function is and how they interact.

Take this object for example:

var foo = {
  bar : 'hello',
  sayBar : function(){
    alert(this.bar);
  }
};

      

You can use it as it is and as you would expect. You can reassign foo.bar

and use the method sayBar

without any problem:

foo.bar = 'Goodbye';
foo.sayBar(); //alerts(Goodbye);

      

Now say that you need to have a bunch of objects that require a property bar

, but with different values. This is when you can create a constructor to create unique instances:

var fooClass = function(newbar){
  this.bar = newbar;
};

      

Which can be called like this:

var myFoo = new fooClass('baz');
console.log(myFoo.bar); //logs 'baz'

      

What can be broken like this: "//" →

var fooClass = function(newbar){
  //**IF 'new' is declared ** create an instance object, for now it has no declared prototype other than Object base prototype
  //That instance of the prototype is now scoped to 'this'

  //it sets the 'constructor' of the instance behind the scenes      
  //this.constuctor = [our constructor function]

  // now we can assign values to our instance and invoke it methods

  this.bar = newbar;
  // our instance now has a key 'bar' that has the value of newbar


  // so our instance now looks like this:
  //
  // { 
  //  bar : [newbar]
  // }


  // return our instance object { bar : [newbar] }
};

      



This is where prototype objects are used. Let's use our object foo

as a prototype fooClass

:

var fooClass = function(newbar){
  this.bar = newbar;
};

fooClass.prototype = foo;

      

Now, as you would expect, you can use the methods foo

:

var myFoo = new fooClass('baz');
myFoo.sayBar(); //alerts baz

      

This is what the constructor does:

var fooClass = function(newbar){
  //**IF 'new' is declared ** create an instance object, which uses foo as it prototype
  //That instance of the prototype is now scoped to 'this'

  //it sets the 'constructor' of the instance behind the scenes      
  //this.constuctor = [our constructor function]

  // now we can assign values to our instance and invoke it methods

  this.bar = newbar;
  // we have now overridden foo default foo.bar with our instance bar


  // so our instance now looks like this:
  //
  // { 
  //  bar : [newbar], //overridden in constructor
  //
  //  //with access to foo methods through the prototype chain
  //  sayBar : function(){
  //    alert(this.bar);
  //  }
  // }


  // return our instance object
};

      

There is nothing magical, all a constructor function does is create and return an instance of an object, and everything .prototype =

does the prototype setup for that object.

What happens if I forget "new"?

If you don't use new when using a constructor function, no new object instance is created or returned. So it was often checked if your current instance has an appropriate constructor:

var fooClass = function(newbar){
  if(!(this instanceof fooClass)){ //if this constructor != fooClass
    return new fooClass(newbar); //refire constructor with 'new'
  }

  //...
};

      

+1


source







All Articles