(function($){
var defaults = {
name : 'benjamin'
};
function EasyZoom(target, options) {
this.$target = $(target);
this.opts = $.extend({}, defaults, options, this.$target.data()); //why extend this.$target.data()?
this._init();
}
EasyZoom.prototype._init = function() {
this.printout();
};
EasyZoom.prototype.printout = function(){
console.log(this.opts);
};
$.fn.easyZoom = function(options){
$.data(this, 'easyZoom', new EasyZoom(this, options)); //isn't $.data(name, value) only?, why $.data(this, 'name', object)?
};
})(jQuery);
$(document).ready(function(e) {
$('div').easyZoom({name: 'benny'});
});我刚开始创建jQuery插件,我试着学习easyZoom插件的风格。不过,我有几个问题我不明白
$.data(); ins的数据解析应该是name & value,为什么在easyZoom插件中使用this, name, object$.extend();为什么将$target.data();对象扩展到缺省值?var foo = new EasyZoom(this, options);发布于 2017-01-20 05:44:43
首先,它是$.data(elem, key, value),而不是$.data(name, value)。
其次,$.extend()喜欢合并。
举个例子
$.extend({}, {'foo': '1'}, {'hoge': '2'}) => {'foo': '1', 'hoge': '2'}您可以检查jQuery的文档。
https://stackoverflow.com/questions/41756765
复制相似问题