//第1种写法
function Circle(r) {
//对象属性
this.R = r;
}
//对象静态属性
Circle.PI = 3.1415926;
//对象静态方法
Circle.prototype.area = function () {
return Circle.PI * this.R * this.R
};
var c = new Circle(1.0);
alert(c.area());
function Circle(r) {
//对象属性
this.R = r;
}
//对象静态属性
Circle.PI = 3.1415926;
//对象静态方法
Circle.prototype.area = function () {
return Circle.PI * this.R * this.R
};
var c = new Circle(1.0);
alert(c.area());
//第2种写法
var Circle = function () {
var obj = new Object();
//对象属性
obj.PI = 3.1415926;
//对象方法
obj.area = function (r) {
return this.PI * r * r
}
return obj;
}
var c = new Circle();
alert(c.area(1.0));
var Circle = function () {
var obj = new Object();
//对象属性
obj.PI = 3.1415926;
//对象方法
obj.area = function (r) {
return this.PI * r * r
}
return obj;
}
var c = new Circle();
alert(c.area(1.0));
//第3种写法
var Circle = new Object();
//对象静态属性
Circle.PI = 3.1415926;
//对象静态方法
Circle.Area = function (r) {
return this.PI * r * r;
};
alert(Circle.Area(1.0));
var Circle = new Object();
//对象静态属性
Circle.PI = 3.1415926;
//对象静态方法
Circle.Area = function (r) {
return this.PI * r * r;
};
alert(Circle.Area(1.0));
//第4种写法
var Circle = {
//对象静态属性
"PI": 3.1415926,
//对象静态方法
"area": function (r) {
return this.PI * r * r;
}
};
alert(Circle.area(1.0));
var Circle = {
//对象静态属性
"PI": 3.1415926,
//对象静态方法
"area": function (r) {
return this.PI * r * r;
}
};
alert(Circle.area(1.0));
//第5种写法
var Circle = new Function("this.PI = 3.14159;this.area = function( r ) {return r*r*this.PI;}");
alert(new Circle().area(1.0));
var Circle = new Function("this.PI = 3.14159;this.area = function( r ) {return r*r*this.PI;}");
alert(new Circle().area(1.0));