Birnou (1) [Avatar] Offline
#1
Hello All,

I think that there is an approach error on the comparison between class-based and prototype-based.
about the sentence and probation variables....shouldn't they be static in the class-based approach to be a class variable and not instance variable.

Indeed, if in the prototype based one change the sentence value... it can be confusing...

in the class based approach, the sentence and probation variables are stored in within the instance itself... in the prototype approach they are stored in the prototype object.. except if one change their value from the instance... in that case one get two different variable overriding themself...

don't you think ?

// logs 4
console.log(firstPrisoner.sentence)

firstPrisoner.sentence = 'foo'
// logs 'foo'
console.log(firstPrisoner.sentence)

// logs 4
console.log(Prisoner.prototype.sentence)

have a nice day
birnou
michael.mikowski (247) [Avatar] Offline
#2
Re: Page 38 - Table 2-3 Multiple objetcs: class vs prototype
Hi Birnou:

It is certainly true you can override a prototype property with an instance copy. Further, properties can be overwritten in at any level of a prototype chain. For example, consider a "cat" object. It's prototype chain might look like this:

Object -> Animal -> Mammal -> Cat -> Instance

The toString method property can be overwritten at any one of these levels. The most important takeaway is this: the property set closest to (or on) the Instance wins.

I don't think there is a problem with 2.3, as that the prototype chain is covered in detail in section 2.5.1.

Cheers, Mike
callahanp (10) [Avatar] Offline
#3
Re: Page 38 - Table 2-3 Multiple objetcs: class vs prototype
After reading The list of four steps for prototype-based objects in Table 2.3 Multiple objects: class versus prototype and later encountering your recommended factory method later in the section, some things are unclear:

While the example using new clearly shows the four steps you list in the table, later you state:
"Object.create has been developed" and go on to use it in a factory method.

Reading the section Object Create for older browsers seems clarify things a bit, but somehow the list of 4 steps doesn't seem to line up with what's happening in the final pattern using Object.Create in a factory function. Although the elements of all four steps are covered, its not clear where steps 2 and 3 are done, if the order of steps is the same, or if one of the steps is split between the Object.create call and the factory function.

At least it wasn't until I read the section Object.create for older browsers.

It might make sense to follow the object creation using the factory method in terms of what needs to be done to create an object. (your four steps)

Another thing that I found confusing in this section is "What exactly is the object?" the thing returned by Object.create, or the thing returned by the factory method. Possible answer: both are objects. Does the text need something to distinguish the instantiation of the "prototype object" from the completion of the final object we want to work with?

As for the placement of this material in Chapter 2, I can see how I would get very lost later in the book without it. I've read other books where the fundamentals are not separated out like this and they tend to get lost. With this material concentrated in a single chapter, you can focus on what you need to learn to understand the later material. If you left all of this for later, it would be distracting to struggle with basic concepts at the same time as you are trying to learn how the concepts are applied.

Thanks.