【发布时间】:2014-09-28 18:40:54
【问题描述】:
这段代码应该做一个简单的任务,即计算两点之间的距离。实际上,我编写这段代码是为了了解 get 和 set 是如何工作的,因为我是这个概念的新手。但它不断给我错误,例如出现了意外的逗号/分号。我无法找出实际问题所在。
我还有一个问题,如果我想为 x 和 y 变量设置新值,我该如何实现?我的意思是我可以将 set 属性视为一个函数并简单地发送值 points.addition(5,6 ,7,8) ?
(function(){
function Point(x1,x2,y1,y2){
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
Point.prototype={
get addition(){
return Math.sqrt((this.x2-this.x1)+(this.y2-this.y1));
},
set addition(x1,x2,y1,y2){
this.x1=x1;
this.x2=x2;
this.y1=y1;
this.y2=y2;
}
};
var points=new Point(1,2,3,4);
console.log(points.addition);
})();
【问题讨论】:
标签: javascript get set