就是实现复制的一个过程
(function() { var Explode = function(container, params) { ‘use strict‘; var n = this; if(!(this instanceof Explode)) return new Explode(container, params); var defaults = { img: ‘chi2.png‘, type: 1 }; params = params || {}; /*有参数传递进来赋给params,没有为空*/ var originalParams = {}; /*备份传进来的params*/ for(var param in params) { /*遍历params*/ if(typeof params[param] === ‘object‘) { /*如果是对象,继续遍历该对象,再赋值*/ originalParams[param] = {}; for(var deepParam in params[param]) { originalParams[param][deepParam] = params[param][deepParam]; } } else { /*不是对象,直接赋值*/ originalParams[param] = params[param]; } }; for(var def in defaults) { /*遍历默认的参数,如果传入的参数没有定义到,则把默认值加进来,若已定义,则默认值没用*/ if(typeof params[def] === ‘undefined‘) { /*比如:如果params[img]没有定义,则使用默认值*/ params[def] = defaults[def]; } else if(typeof params[def] === ‘object‘) { for(var deepDef in defaults[def]) { if(typeof params[def][deepDef] === ‘undefined‘) { params[def][deepDef] = defaults[def][deepDef]; } } } }; n.params = params; n.originalParams = originalParams; n.container = typeof container === ‘string‘ ? document.querySelectorAll(container) : container; if(!n.container || (n.container.length && n.container.length == 0)) return; /*没有container直接return*/ if(n.container.length && n.container.length > 1) { /*有container且个数大于1*/ var s = []; for(var i = 0; i < n.container.length; i++) { s.push(new Explode(n.container[i], params)); }; return s; }; n.container = n.container[0] || n.container; /*有且仅有一个container*/ })()
时间: 2024-10-07 06:15:13