File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javascript prototype question

 
Chrix Wu
Ranch Hand
Posts: 121
  • 0
  • 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: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
  • 0
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic