;
"use strict";
function IPoint() {
this.getDistance = function(point) {
var x = this.x - point.x;
var y = this.y - point.y;
return Math.sqrt(x * x + y * y);
};
this.isAbove = function(point) {
if (this.y <= point.y) {
return true;
} else {
return false;
}
};
this.isBelow = function(point) {
if (this.y >= point.y) {
return true;
} else {
return false;
}
};
this.isLeftOf = function(point) {
if (this.x <= point.x) {
return true;
} else {
return false;
}
};
this.isRightOf = function(point) {
if (this.x >= point.x) {
return true;
} else {
return false;
}
};
};
var Point = function(x, y) {
this.x = x;
this.y = y;
IPoint.call(this);
};
Point.prototype =
Object.create(null,
{
get x() {
return this._x;
},
set x(v) {
this._x = v;
},
get y() {
return this._y;
},
set y(v) {
this._y = v;
},
});
给我错误 Uncaught TypeError:属性描述必须是一个对象:undefined geometry.js:47(匿名函数)。这让我觉得我不能在dot.create中传递的对象中使用setter和getter,但我不知道为什么。我做错了什么?
答案 0 :(得分:4)
Object.create
确实将property descriptors的对象作为其第二个参数,就像defineProperties
一样。正确的语法是
Point.prototype = Object.create(null, {
x: {
get: function() { return this._x; },
set: function(v) { this._x = v; },
// configurable: true,
// enumerable: true
},
x: {
get: function() { return this._y; },
set: function(v) { this._y = v; },
// configurable: true,
// enumerable: true
}
});
但是,我没有看到Point
s不应该从Object
继承的原因,所以就这样做吧
Point.prototype = {
get x() { return this._x; },
set x(v) { this._x = v; },
get y() { return this._y; },
set y(v) { this._y = v; }
};