jQuery插件开发前准备(三)

【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 || {});
posted @ 2016-08-17 22:10  宋小玉  阅读(209)  评论(0)    收藏  举报