1. 程式人生 > >插件編寫

插件編寫

處理 mar fun object xtend urn logs () clas

參數處理

JS:

function plugin_mian_func(options){
  var defaluts = {
    opt1: ‘opt1‘,
    opt2: ‘opt2‘,
    opt3: {
      opt3_1: ‘opt3_1‘,
      opt3_2: ‘opt3_2‘,
    }
  };

  // 覆蓋插件默認參數
  options = (function(){
    // IE不支持Object.assign
    return Object.hasOwnProperty(‘assign‘) ? Object.assign({},defaluts,options) : (function(){
      for(var name in options){
        defaluts[name] = options[name];
      }
      return defaluts;
    })(defaluts, options)
  })(defaluts, options);
  
  console.log(options);
}
// 測試
plugin_mian_func({opt1:‘user_opt1‘,opt3: {opt3_1: ‘user_opt3_1‘, opt3_3: ‘user_opt3_1‘}});

jQuery:

function plugin_mian_func(options){
  var defaluts = {
    opt1: ‘opt1‘,
    opt2: ‘opt2‘,
    opt3: {
      opt3_1: ‘opt3_1‘,
      opt3_2: ‘opt3_2‘,
    }
  };

  // 使用jQuery.extend 覆蓋插件默認參數
  options = $.extend({}, defaluts, options);
  console.log(options);
}
// 測試
plugin_mian_func({opt1:‘user_opt1‘,opt3: {opt3_1: ‘user_opt3_1‘, opt3_3: ‘user_opt3_1‘}});

插件編寫