Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm learning javascript.

I see that with an object, I can use .prototype, is it fair to say that, in a class/tree hierarchy I am effectively using the 'parent' (or 'ancestor' perhaps) by doing that? Can a class only have 1 prototype at a time as in Ruby?

Call it Personal Homework that I set myself.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Yes, the prototype property on a given object refers to the parent object, which allows the given object to inherit all its properties and methods. However, the definition of parent probably should be defined here. In a prototype-based language, that means that a single object can be the parent of many other objects. A side effect of this is that a property that changes in the parent is reflected in all child objects, assuming the child objects don't override it. This essentially forms a tree structure of objects that you can traverse. This is different then in a class-based language where the parent refers to the super class, and does not represent a tree of objects. The object also inherits the properties and methods from the parent, but it is all self contained within that one object.

share|improve this answer
    
In some class-based languages (such as Java), classes can have static attributes, which are essentially attached to the class object and only indirectly to the child object. –  Matthew Flynn Mar 23 '12 at 18:38
    
That's good to know Matthew - if they are attached indirectly, how do you access them? Is there a special notation? Do you have to use super or something? –  Michael Durrant Mar 23 '12 at 18:41
1  
@MichaelDurrant: In Java, static fields and methods are accessible by the class name. (You can access them from object instances, but that is generally discouraged because nothing about the object instance is relevant in a static context.) So if there was a class Foo, then you could call the static method bar by saying: Foo.bar(). Static fields and methods are inherited, but cannot be overidden. –  Jeremy Heiler Mar 23 '12 at 18:44
    
The prototype property does not inherit. The [[Prototype]] is the parent of any object and determines what is inherited –  Raynos Mar 24 '12 at 19:27
    
@Raynos That's what I meant. Sorry. –  Jeremy Heiler Mar 24 '12 at 22:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.