我可以将混合模式与二传手和吸气手一起使用吗?
Can I use the mixin pattern with setters and getters
;
"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;
},
});
给我错误未捕获的类型错误:属性描述必须是对象:未定义的几何.js:47(匿名函数(。这给我的印象是,我不能在 dot.create 中传递的对象中使用二传手和 getter,但我不知道为什么。我做错了什么?
Object.create
确实将属性描述符的对象作为其第二个参数,就像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
不应该从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; }
};
相关文章:
- 转义符不能与innerHTML一起使用
- 混合 ui-sref 和 $state.go 在 Angular ui-router 中进行状态转换
- 高亮显示与数组字符串一起使用时文本插件中断
- setTimeout可以与闭包内的函数一起使用吗
- 将依赖外部库的UMD模块与browserfy捆绑在一起
- Highcharts colorsByPoint与系列中的线性渐变一起使用时不起作用
- 如何将js库与typescript一起使用
- 将webpack和babel与父项目目录中的文件一起使用
- 将 Drag&Drop 与 jsTree 和 DataTables 一起使用
- if语句混合在一起
- 三.js - 将WebGL和CSS3D与iFrame混合在一起
- 将Ajax与Javascript混合在一起
- 如果将php和javascript混合在一起不起作用,请使用简写
- 将字符串与数组变量混合在一起,为javascript中的元素分配id
- 我可以将混合模式与二传手和吸气手一起使用吗?
- 我有点困惑,我怎么能把我的三个函数混合在一起,使它变短
- 从概念上讲,我如何编写通量存储以避免将不相关的数据混合在一起
- 将javascript和jquery与XSL转换或函数参数混合在一起的问题
- 是否有可能将多个音频文件混合在一起,最好使用javascript
- 可以使用javascript将两个SVG文件混合在一起,从而生成HTML/HTML5