JS 面向对象的5钟写法

<script language="javascript" type="text/javascript">
//第1种写法
function Circle(r) {
this.r = r;
}
Circle.Aer  = 9.18;
Circle.PI = 3.14159;
Circle.prototype.
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.14159;
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.14159;
Circle.Area = function( r ) {
return this.PI * r * r;
}
alert( Circle.Area( 1.0 ) );

//第4种写法
var Circle={
"PI":3.14159,
area:function(r){
return this.PI * r * r;
}
};
alert( Circle.area(1.0) );

</script>

你可能感兴趣的:(面向对象)