[FMX][OOP] whats the diff

whats the difference between…


MyClass.prototype = MovieClip.prototype;

and…


MyClass.prototype = New MovieClip();

thanx M64

:red: <-- lol

lol forgot about this theard :stuck_out_tongue:

[size=1]still like to know the answer :stuck_out_tongue: [/size]

The difference lays in the fact the when using


MyClass.prototype = MovieClip.prototype;

You are setting the prototype object of MyClass to the prototype object of the MovieClip class, thus ensuring that every MyClass instance has access to the prototype object of the MovieClip class.
However, if you use


MyClass.prototype = New MovieClip();

You are not only enabling every MyClass instance access to the prototype object of the MovieClip class, you are also placing the properties of the MovieClip class instance in the prototype object of MyClass. This doesn’t happen when just setting the prototype:


SuperClass = function(){
	this.property = "Property";
}
SuperClass.prototype.traceHello = function(){
	trace("Hello");
}
SubClass = function(){
}
SubClass.prototype = SuperClass.prototype
SubInstance = new SubClass();
SubInstance.traceHello()
trace(SubInstance.property)
// Hello
// undefined


SuperClass = function(){
	this.property = "Property";
}
SuperClass.prototype.traceHello = function(){
	trace("Hello");
}
SubClass = function(){
}
SubClass.prototype = new SuperClass();
SubInstance = new SubClass();
SubInstance.traceHello()
trace(SubInstance.property)
// Hello
// Property

This is a nice example of the proto property. As you may know, proto indicates where to search next if a varaible isn’t found. property was defined for the SuperClass, and then an instance of a SuperClass has been placed in the prototype object of the SubClass. This means that every SubClass also has access to ‘property’ via it’s prototype object.

When you trace SubInstance.property, it will go looking in the SubInstance itself first, and when it doesn’t find it there, it will use the proto property and go looking in the prototype object of SubClass, where it finds property::


SuperClass = function(){
	this.property = "Property";
}
SuperClass.prototype.traceHello = function(){
	trace("Hello");
}
SubClass = function(){
}
SubClass.prototype = new SuperClass()
SubInstance = new SubClass();
SubInstance.traceHello()
trace(SubInstance.property)
trace(SubInstance.__proto__ == SubClass.prototype)
// Hello
// Property
// true

So if you would’ve defined property also in the SubClass constructor, it won’t go looking in the prototype object because it will already find it in the SubInstance itself:


SuperClass = function(){
	this.property = "Property";
}
SuperClass.prototype.traceHello = function(){
	trace("Hello");
}
SubClass = function(){
	this.property = "Sub Property"
}
SubClass.prototype = new SuperClass()
SubInstance = new SubClass();
SubInstance.traceHello()
trace(SubInstance.property)
// Hello
// Sub Property

Hope this cleared things up :slight_smile:

that reminds me… my oop tutorial is still floating about around here somewhere needing to be posted :open_mouth:

So


MyClass.prototype = New MovieClip();

is better then


MyClass.prototype = MovieClip.prototype;

as it can find whats in the constructor all so :slight_smile:

and the “Sub Property” thing… i knew that :stuck_out_tongue: :smiley:

lol forgot this thead again 8-]

thanx Voets :wink:

Geen probleem :wink: