JavaScript构造函数模式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JavaScript构造函数模式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2534字,纯文字阅读大概需要4分钟。
内容图文
![JavaScript构造函数模式](/upload/InfoBanner/zyjiaocheng/725/4cd7ec0b16584634995eb0ec90a62d11.jpg)
我正在寻找一个理智的解决JavaScript只有一个构造函数的问题.所以我们假设我们有一个Point类,我们希望允许从坐标创建对象.
我会在所有这些例子中忽略类型检查.
function Point(x, y) {
this.x = x;
this.y = y;
}
简单.如何从其他点创建积分?
function Point(x, y) {
if (!y /* && x instanceof Point */) {
y = x.y;
x = x.x;
}
this.x = x;
this.y = y;
}
这很快变成了一场噩梦.所以我想要的是一个设计模式,它将这两个构造函数分离(或者将一个分成两个,相反). Objective-C有一个很好的模式. ObjC人用某物创造物体.
function Point(x, y) {
this.x = x;
this.y = y;
}
Point.withPoint = function(point) {
return new Point(point.x, point.y);
};
到目前为止,我非常喜欢这个.但现在我们有两种不同的语法.
var a = new Point(4, 2);
var b = Point.withPoint(a);
好吧,这很容易,不是吗?只需添加Point.withCoordinates.但那么构造函数呢?把它藏起来?我不知道.我想这就是你进来的地方.
这就是我决定采用的方式:
var Point = {
withCoordinates: function(x, y) {
if (typeof x == 'number' && typeof y == 'number') {
this.x = x;
this.y = y;
return this;
}
throw TypeError('expected two numbers');
},
withPoint: function(point) {
if (typeof point.x == 'number' && typeof point.y == 'number') {
this.withCoordinates(point.x, point.y);
return this;
}
throw TypeError('expected a point');
}
};
var a = Object.create(Point).withCoordinates(0, 0);
var b = Object.create(Point).withPoint(a);
优点:
>没有样板
>描述性语法/ API
>很好地扩展
>功能性
>易于测试
缺点:
>实例不知道它们是否已初始化
>不能只为类添加属性(比较Number.MAX_SAFE_INTEGER)
注意Point.withPoint中的类型检查.它允许点击事件等鸭类点.
function onClick(event) {
var position = Object.create(Point).withPoint(event);
}
还要注意在某种默认ctor中缺少零初始化.积分实际上是一个非常好的例子,为什么这并不总是一个好主意.
解决方法:
就像在ObjC上一样,你可以有单独的“alloc”和“init”条目,例如:
function Point() {}
Point.prototype.withCoordinates = function(x, y) {
this.x = x;
this.y = y;
return this;
}
Point.prototype.withOffsetFromPoint = function(p, delta) {
this.x = p.x + delta;
this.y = p.y + delta;
return this;
}
p = new Point().withOffsetFromPoint(
new Point().withCoordinates(5, 6),
10);
console.log(p) // 15, 16
其中虚拟构造函数基本上是“alloc”的东西.
同样以更现代的方式,没有新的:
Point = {
withCoordinates: function(x, y) {
this.x = x;
this.y = y;
return this;
},
withOffsetFromPoint: function(p, delta) {
this.x = p.x + delta;
this.y = p.y + delta;
return this;
}
}
p = Object.create(Point).withOffsetFromPoint(
Object.create(Point).withCoordinates(5, 6),
10);
console.log(p)
另一个(也许是最惯用的)选项是使构造函数接受命名参数(通过“options”对象):
p = new Point({ x:1, y:2 })
p = new Point({ point: someOtherPoint })
内容总结
以上是互联网集市为您收集整理的JavaScript构造函数模式全部内容,希望文章能够帮你解决JavaScript构造函数模式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。