这个关键字在匿名函数/构造函数中

时间:2015-07-09 21:35:38

标签: javascript

在某种程度上,我知道代码中发生了什么,只是为了清除我对这个问题的怀疑

的JavaScript

Point = function (x, y)     //Here anonymous constructor is define 
{
    this.x = x;
    this.y = y;
}

var points=[] 
points.push(new Point(centerX + radius * Math.sin(angle),centerY - radius * Math.cos(angle)));  //object is created and push in the array

要访问points数组的值,我可以写点[i] .x?

2 个答案:

答案 0 :(得分:0)

正确,您可以使用其索引i访问该对象,然后您取消引用该对象以访问它的属性/成员

var Point = function (x, y)
{
    this.x = x;
    this.y = y;
}

var points = [];
points.push(new Point(1, 2));
var point = points[0];
alert(point.x == point[0].x);

答案 1 :(得分:0)

是的,请查看

   var Point = function (x, y)     //Here anonymous constructor is define
{
    this.x = x;
    this.y = y;
}

var points = [];
points.push(new Point(2,5));
points.push(new Point(3,11));
points.push(new Point(9,1));

for(var i = 0; i <points.length; i++){
console.log(points[i].x);
console.log(points[i].y);
};