简体   繁体   English

简单的面向对象的JavaScript示例

[英]simple object-oriented JavaScript example

I'm learning OO JavaScript (again). 我正在学习OO JavaScript(再次)。 I've written this simple object 我已经写了这个简单的对象

function circle(){
  this.radius = 4;
}

circle.prototype.area = function(){
  this.radius * this.radius * 3.14;
};

var c = new circle();
c.area();

The value returned by c.area() is undefined . c.area()返回的值是undefined I guess this can only because this.radius is not returning 4, why not? 我想这只能是因为this.radius不返回4,为什么不呢?

radius has the value of 4 , but the area method doesn't return any value. radius的值为4 ,但是area方法不返回任何值。

circle.prototype.area = function(){
  return this.radius * this.radius * 3.14;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM