【MyPlugin核心函数实现】
从上两节中我们已经知道了MyPlugin是个构造函数,需要这$.fn.MyPlugin()中实例化,所以MyPlugin应该定义如下:
var MyPlugin = (function() { function MyPlugin(element, options) { // 将用户配置项与默认选项进行深拷贝 this.settings = $.extend(true, $.fn.MyPlugin.defaultValue, options || {}); this.element = element; this.init(); } MyPlugin.prototype = { init: function() { } //more };
// 必须要将该对象返回出去 return MyPlugin; })();
因为我们写的插件,很多时候需要有默认值和用户自定义值,所以就需要提供接口给其他开发调用。
$.fn.MyPlugin.defaultValue = { // 圆大小 size: '25', // 环大小 border: '5', // 环背景 bgColor: '#CCC', // 进度背景 frontColor: '#008000', // 进度条字体大小 fontSize: '12px' };
通过深拷贝的方式,将用户设定值,和默认值整合在一起
this.settings = $.extend(true, $.fn.MyPlugin.defaultValue, options || {});
本文深入解析了MyPlugin构造函数的实现方式,介绍了如何通过深拷贝整合用户配置与默认设置,实现jQuery插件的自定义功能。文章详细展示了MyPlugin核心函数的代码结构,并解释了如何设置默认参数。

被折叠的 条评论
为什么被折叠?



