我刚开始试图让我的头绕着原型范例来制作一个jquery插件。单独文件问题的JS prototype.namespace
像这样我的代码工作:
(function(window, $){
var Remote = function(elem, options) {
this.elem = elem;
this.$elem = $(elem);
this.options = options;
this.p = [];
};
Remote.prototype = {
defaults: {
message: 'Hello world!'
},
init: function() {
this.config = $.extend({}, this.defaults, this.options, this.metadata);
...
},
findPath: function() {
var t = this.p.length;
...
}
};
Remote.prototype.constructor = Remote;
Remote.defaults = Remote.prototype.defaults;
$.fn.remote = function(options) {
return this.each(function() {
new Remote(this, options).init();
});
};
})(window, jQuery);
所以现在我试图让它多了几分模块化,具有拆分成多个文件的能力(请纠正我,如果我要去任何这个错误)的:
var Remote = function(elem, options) {
this.elem = elem;
this.$elem = $(elem);
this.options = options;
this.p = [];
};
Remote.prototype = {
defaults: {
message: 'Hello world!'
},
init: function() {
this.config = $.extend({}, this.defaults, this.options, this.metadata);
...
}
};
// I'd like to put this object into path.js
Remote.prototype.path = {
find: function() {
var t = this.p.length;
// this.p is undefined
...
}
};
Remote.prototype.constructor = Remote;
Remote.defaults = Remote.prototype.defaults;
(function(window, $){
$.fn.remote = function(options) {
return this.each(function() {
new Remote(this, options).init();
});
};
})(window, jQuery);
通过内Remote.prototype
命名空间我似乎已经失去范围与this
。
Q1 - 哪里有this
不见了?第二季度 - 这是模块化我的代码的最佳方式,所以我可以将它分解成不同的文件。
谢谢。
感谢您的链接。这东西很混乱!这似乎很清楚,它只是没有像我一直在阅读关于jQuery插件.... – daviestar
如果任何人有兴趣,我认为这是最清楚的解释:http://pivotallabs.com/javascript-constructors-prototypes - 和 - 的 - 新的关键字/ – daviestar