//第1种写法
function Circle(r) {
this.r = r;
}
Circle.PI = 3.14159;
Circle.prototype.area = function() {
return Circle.PI * this.r * this.r;
}
var c = new Circle(1.0);
alert(c.area());
?
Java代码
//第2种写法??? ??
var Circle = function() {??? ??
??? var obj = new Object();??? ??
??? obj.PI = 3.14159;??? ??
?????? ??
??? obj.area = function( r ) {??? ??
???????returnthis.PI * r * r;??? ??
??? }??? ??
???return obj;??? ??
}??? ??
?? ??
var c = new Circle();??? ??
alert( c.area( 1.0 ) );????
//第2种写法
var Circle = function() {
var obj = new Object();
obj.PI = 3.14159;
obj.area = function( r ) {
return this.PI * r * r;
}
return obj;
}
var c = new Circle();
alert( c.area( 1.0 ) );