Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javascript prototype question

 
Chrix Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is an example from <profession javascript for web develper > book,

in this example, prototype properties act like static properties in java, that is , all objects share the same property.

if oCar1 change the array, oCar2 gets the new array too.

BUT the question is, if i change the color of oCar1, why oCar2 stay unchange? why is that?? see below codes:



 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chrix Wu wrote:in this example, prototype properties act like static properties in java, that is , all objects share the same property.

No, that's not the correct way to think of the prototype.

Conceptually, it's more like setting a bunch of instance variables the same way in the constructor. Once the variables are set into an instance, they belong only to that instance and can be changed individually.

That's conceptually, the actual implementation is a bit more complex. But you should probably just get familiar with the concept first.
 
Chrix Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Chrix Wu wrote:in this example, prototype properties act like static properties in java, that is , all objects share the same property.

No, that's not the correct way to think of the prototype.

Conceptually, it's more like setting a bunch of instance variables the same way in the constructor. Once the variables are set into an instance, they belong only to that instance and can be changed individually.

That's conceptually, the actual implementation is a bit more complex. But you should probably just get familiar with the concept first.


I see. thanks bear
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic