• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

javascript prototype question

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:



 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic