【JavaScript】两种常见JS面向对象写法
基于构造函数
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);
console.log(c.area())//3.14159;
类Json写法
var Circle={
"PI":3.14159,
"area":function(r){
return this.PI * r * r;
}
};
调用
console.log(Circle.area(1.0));//3.14159