View Full Version : methods and properties in javascript
kentyler
04-24-2003, 06:14 AM
it seems that you can use prototypes to add class properties but not to add class methods
<canvas debug="true">
<script>
<![CDATA[
function Circle(radius)
{
this.r = radius
}
Circle.PI = 3.14159; //this assignment doesn't work
Circle.prototype.pie = 3.14; // this assignment works
function Circle_Area() { return Circle.pie * this.r * this.r }
Circle.prototype.area = Circle_Area; // assigning an instance method
c = new Circle(5);
debug.write("the radius of the circle is " + c.r); //instance property
debug.write("pie is " + c.pie); // class property
debug.write("PI is " + c.PI); // returns 'reference to undefined property'
debug.write("the circle's area is " + c.area); //returns blank
]]>
</script>
</canvas>
kentyler
04-24-2003, 06:33 AM
there was an error in that last code example. it turns out that you can assign a class method. however, the methods don't seem to have access to class properties. in this code the circle class knows its PI and can calculate its diameter, but when asked to run a method that uses a reference to the class variable PI it returns a "reference to unknown property" error. quite possibly this works and I just have another coding error, but I can't see it.
<script>
<![CDATA[
function Circle(radius)
{
this.r = radius
}
Circle.PI = 3.14159; //this assignment doesn't work
Circle.prototype.pie = 3.14; // this assignment works
function Circle_Area() { return Circle.pie * this.r * this.r; }
function Circle_Diameter() { return this.r * 2; }
Circle.prototype.area = Circle_Area;
Circle.prototype.diameter = Circle_Diameter;
c = new Circle(5);
debug.write("the radius of the circle is " + c.r); //instance property
debug.write("pie is " + c.pie); // returns 3.14
debug.write("PI is " + c.PI); // returns reference to undefined property 'PI'
debug.write("the circle's area is " + c.area()); // returns reference to undefined property 'pie'
debug.write("the circle's diameter is " + c.diameter());
]]>
</script>
antun
04-24-2003, 08:26 AM
Hey Ken
I think you have to use the same Constructor.prototype.name syntax to get at the class'es properties, so you can either say:
function Circle_Area() { return Circle.prototype.pie * this.r * this.r; }
OR refer to the instance
function Circle_Area() { return this.pie * this.r * this.r; }
Is that what you were looking for?
Also, you might find it helpful to enclose your code snippets in the special code tags (see here: http://www.laszlosystems.com/developers/community/forums/misc.php?action=bbcode#buttons). That way the whitespaced won't get stripped.
-Antun
kentyler
04-24-2003, 08:47 AM
thanks, that makes things a lot clearer. My book, JavaScript the Definitive Guide, uses that kind of notation, ie Circle.pie, where the method or property added to the prototype is referred to by referring to the object and then a dot, in many examples, none of them seem to work in Laszlo, so that's a good thing to figure out.
By confirming or adding to what I find by experimenting you are greatly expanding what I learn, thanks again
vBulletin® v3.8.4, Copyright ©2000-2012, Jelsoft Enterprises Ltd.