jQuery in Action, 2nd edition*
The moose likes HTML, CSS and JavaScript and the fly likes javascript prototype question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "javascript prototype question" Watch "javascript prototype question" New topic
Author

javascript prototype question

Chrix Wu
Ranch Hand

Joined: Nov 15, 2009
Posts: 121


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:




** SCJP 5.0 84% **
** SCWCD 1.5 76% **
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Chrix Wu
Ranch Hand

Joined: Nov 15, 2009
Posts: 121
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
 
subject: javascript prototype question