Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I think you're wrong about the constructor's "prototype" property #166

Open
ivanjuras opened this issue Jun 18, 2015 · 1 comment
Open

Comments

@ivanjuras
Copy link

Hey Addy, I've been reading your Javascript design patterns online, and I think it's a marvelous book. I respect you a lot, so don't think that I'm attacking you ( or your knowledge of Javascript ) in any way, shape or form. In fact, I really do admire your knowledge.

In the "Constructors with Prototypes" subsection you say this:

Functions, like almost all objects in JavaScript, contain a "prototype" object. When we call a JavaScript constructor to create an object, all the properties of the constructor's prototype are then made available to the new object.

I don't think that this is right. I think that the "prototype" property actually points to the prototype object of all objects instantiated by the constructor, but not the constructor's prototype itself.

I did a test today in Developer Tools, and this really seems to be the case.

Take this for an example; we have a constructor function ( I'm going to use your example if you don't mind ):

function Car( model, year, miles ) {

  this.model = model;
  this.year = year;
  this.miles = miles;

}

Now, if I check the prototype of this function in Chrome Developer Tools,

Car.__proto__

... I will get this:

function Empty() {}

, which is the uppermost prototype of all functions. Now, I can instantiate the Car 'class':

var myFord = new Car( 'My Ford', 2015, 0 );

, and then see its prototype:

myFord.__proto__

...then I get this:

Car{}

So, I don't think that the function's "prototype" property actually points to it's own prototype; rather, it point's to the prototype of all objects that were instantiated by it.

@yuyokk
Copy link
Contributor

yuyokk commented Aug 11, 2015

@ivanjuras I think what was meant here

function Car(model, year, miles) {
  this.model = model;
  this.year = year;
  this.miles = miles;
}

Car.prototype.testProp = 10;
Car.prototype.getModel = function() {
    return this.model;
}

var car = new Car('SuperCar', 2015, 10);

console.log(car.getModel()); // 'SuperCar'
console.log(car.testProp); // 10

So all the methods and properties assigned to the Function becomes available on the objects created with this construction Function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants