Javascript将对象传递给函数原型
Javascript passing in object to function prototype?
我正在尝试通过创建自己的MVC库来深入研究javascript。我正在研究骨干.js源代码 https://github.com/jashkenas/backbone/blob/master/backbone.js
当你在主干中定义一个集合时.js你会把它分配给一个javascript变量,就像这样var AppCollection = Backbone.Collection.extend({ model: BACKBONE_MODEL })
在内部,创建者使用下划线并将Collection
的prototype
传递到下划线的_.extend()
方法中,如下所示_.extend(Collection.prototype, Events, {})
,空对象参数位于我编写的示例中,作者为集合对象添加所有方法,即值是库前面定义的模型对象的model
键。
我很好奇,如何避免下划线依赖并为集合添加我自己的原型方法,特别是如何将带有键model
的对象传递到我自己的集合对象中?
这是我到目前为止所拥有的。
(function(Pigeon, root, factory) {
root[Pigeon] = factory();
} ('Pigeon', this, function() {
var Pigeon = {};
// Model
var Model = Pigeon.Model = function() {
this.attributes = {};
};
// Collection
var Collection = Pigeon.Collection = function(models) {
};
// View
Pigeon.View = {};
// Controller
Pigeon.Controller = {};
return Pigeon;
}));
为了防止依赖"_",您可以使用以下方法来扩展对象或类,而不是"_.extend()"。
您可以使用Object.assign 来扩展原型。 这样做的问题是并非所有浏览器都完全支持它(我在看你的IE)。 另一种选择是创建自己的扩展函数。 你可以使用 Object.keys(mixinClass) 来做到这一点。
使用 Object.assign:
function a() {
this.propOne = "one";
}
a.prototype.methodOne = function() {
document.write("<br/>I am methodOne from a");
}
function b() {
this.propTwo = "two";
}
b.prototype.methodTwo = function() {
document.write("<br/>I am methodTwo from b");
}
function assigned() {
// Call both "super" constructors (order can matter depending on your case)
a.call(this);
b.call(this);
}
// Assign other object properties to your object and set constructor
Object.assign(assigned.prototype, a.prototype, b.prototype);
assigned.constructor = assigned;
var x = new assigned();
document.write("<pre>I am assigned and I look like: 'n" + JSON.stringify(x, 2, null) + "</pre>");
x.methodOne();
x.methodTwo();
使用自定义扩展函数:
function myExtend() {
var args = Array.prototype.slice.apply(arguments);
var target = args.shift();
var extended = {};
args.forEach(function(o) {
Object.keys(o).forEach(function(k) {
extended[k] = o[k];
});
});
Object.keys(extended).forEach(function(k) {
if (!target[k])
target[k] = extended[k];
});
}
function a() {
this.propOne = "one";
}
a.prototype.methodOne = function() {
document.write("<br/>I am methodOne from a");
}
function b() {
this.propTwo = "two";
}
b.prototype.methodTwo = function() {
document.write("<br/>I am methodTwo from b");
}
function assigned() {
a.call(this);
b.call(this);
}
myExtend(assigned.prototype, a.prototype, b.prototype);
assigned.constructor = assigned;
var x = new assigned();
document.write("<pre>I used myExtend and I look like: 'n" + JSON.stringify(x, 2, null) + "</pre>");
x.methodOne();
x.methodTwo();
这个概念就是混合的概念。 您可以使用它来扩展简单对象或"混合"多个类以创建具有多种类型方法的子类。 不幸的是,这并不完全理想,因为对象实际上具有"supers"属性和方法的副本,而不是使用引用。 无论哪种方式,这都应该适用于您的用例。
- 创建对象函数原型和代码是错误的
- 使用sinon.js创建一个“;“间谍对象”;使用基于真实构造函数/原型的间谍方法
- 使用构造函数 - 原型从文本框输出值
- 为什么函数对象的实例没有继承函数原型属性
- 获取函数原型的源代码
- 挂钩文档.使用函数原型创建元素
- 理解函数原型和__proto__
- 在Javascript的函数原型中创建函数
- Emberjs :哈希中的函数原型
- Javascript函数在父函数原型中带有原型
- 构造函数原型-继承混淆
- 本地作用域函数原型覆盖
- 使用函数原型的 JavaScript 继承问题
- 为什么我的代码认为 canvas 函数在 JavaScript 中的构造函数原型中是未定义的
- 所有内容也都链接到函数原型
- Javascript MDN 函数原型绑定 polyfill 在数组中是可枚举的
- Javascript将对象传递给函数原型
- 我应该使用构造函数/原型吗?
- 为什么函数原型不能修改 .
- 删除函数原型对象