Originally posted by Ironclaw
Still cant understand how:
OOP would help me.Code:Circle.prototype.area = function() { return Math.PI*this.radius*this.radius; }; function Circle(radius) { this.radius = radius; } var myCircle = new Circle(5); var myCircleArea = myCircle.area(); trace(myCircle.area());
well, this is as i would tell it...
Circle is the class
this would be the class constructor, a function which constructs a new instance of the circle classPHP Code:function Circle(radius) {
this.radius = radius;
}
this is a prototype function of the circle class, all instances (all circles) will have access to this function.PHP Code:Circle.prototype.area = function() {
return Math.PI*this.radius*this.radius;
};
An instance of the circle class is constructed with:
the circle class constructor asks for a radius of all its new instances, in this case myCircle has a radius of 5PHP Code:var myCircle = new Circle(5);
as myCircle is an instance of the circle class it has access to the Circle prototype function...
PHP Code:var myCircleArea = myCircle.area();
hope this helps...




Reply With Quote