javascript – Crockford风格的原型模式;寻找一个优雅的解决方案
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – Crockford风格的原型模式;寻找一个优雅的解决方案,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2975字,纯文字阅读大概需要5分钟。
内容图文
在编写JavaScript程序时,我经常使用Crockford的原型模式.我以为我理解了所有涉及的“陷阱”,但我发现了一个我之前没想过的.我想知道是否有人有最好的处理方法.
这是一个简单的例子:
// Here's the parent object
var MyObject = {
registry: {},
flatAttribute: null,
create: function () {
var o, F = function () {};
F.prototype = this;
o = new F();
return o;
}
};
// instance is an empty object that inherits
// from MyObject
var instance = MyObject.create();
// Attributes can be set on instance without modifying MyObject
instance.flatAttribute = "This is going to be applied to the instance";
// registry doesn't exist on instance, but it exists on
// instance.prototype. MyObject's registry attribute gets
// dug up the prototype chain and altered. It's not possible
// to tell that's happening just by examining this line.
instance.registry.newAttribute = "This is going to be applied to the prototype";
// Inspecting the parent object
// prints "null"
console.log(MyObject.flatAttribute);
// prints "This is going to be applied to the prototype"
console.log(MyObject.registry.newAttribute);
我希望看起来对实例做出的任何更改都不会传播继承更改.当属性是对象并且我正在设置嵌套属性时,情况并非如此.
解决方案是重新初始化实例上的所有对象属性.但是,使用此模式的一个明显的优点是从构造函数中删除重新初始化代码.我正在考虑克隆父项的所有对象属性并在create()函数中的实例上设置它们:
{ create: function () {
var o, a, F = function () {};
F.prototype = this;
o = new F();
for (a in this) {
if (this.hasOwnProperty(a) && typeof this[a] === 'object') {
// obviously deepclone would need to be implemented
o[a] = deepclone(this[a]);
}
}
return o;
} };
有没有更好的办法?
解决方法:
这会给你预期的结果吗?这里我没有使用Object文字,而是使用父对象(Base)的即时实例化构造函数:
var Base = ( function(){
function MyObject(){
this.registry = {},
this.flatAttribute = null;
if (!MyObject.prototype.create)
MyObject.prototype.create = function(){
return new this.constructor();
};
}
return new MyObject;
} )(),
// create 2 instances from Base
instance1 = Base.create(),
instance2 = Base.create();
// assign a property to instance1.registry
instance1.registry.something = 'blabla';
// do the instance properties really belong to the instance?
console.log(instance1.registry.something); //=> 'blabla'
console.log(instance2.registry.something === undefined); //=> true
但它有点虚拟.如果您不想使用new运算符(我认为这完全是它的想法),以下为您提供了一种方法,无需创建方法:
function Base2(){
if (!(this instanceof Base2)){
return new Base2;
}
this.registry = {},
this.flatAttribute = null;
if (!Base2.prototype.someMethod){
var proto = Base2.prototype;
proto.someMethod = function(){};
//...etc
}
}
//now the following does the same as before:
var instance1 = Base2(),
instance2 = Base2();
// assign a property to instance1.registry
instance1.registry.something = 'blabla';
// do the instance properties really belong to the instance?
console.log(instance1.registry.something); //=> 'blabla'
console.log(instance2.registry.something === undefined); //=> true
jsfiddle中的示例
内容总结
以上是互联网集市为您收集整理的javascript – Crockford风格的原型模式;寻找一个优雅的解决方案全部内容,希望文章能够帮你解决javascript – Crockford风格的原型模式;寻找一个优雅的解决方案所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。