jquery插件开发
jquery是js的自定义类型,开发jquery插件先要弄懂自定义类型的扩展
function Robot(id, name){ this.id = id; this.name = name; } Robot.prototype = { constructor: Robot, init: function(){} } //自定义类型的静态方法添加: Robot.isOutOfOrder = function(robot){} //使用: var robotOne = new Robot("001", "Wee"); Robot.isOutOfOrder(robotOne); //自定义类型的成员方法添加: Robot.prototype.start = function(){} //使用: var robotTwo = new Robot("002", "Evv"); robotTwo.start();
//jquery静态方法添加: $.extend({ min: function(){} }) //使用: $.min(); //jquery成员方法添加: $.fn.extend({ getMin: function(){} }) //使用: $(".class").getMin(); //ps:$.fn等同于$.prototype
//easy demo: (function (window, $) { var defaults = { }; function robotDance(robotInfo, options) { this.options = $.extend({},defaults,options);this.init(); } robotDance.prototype = { constructor : robotDance, init: function () { } }; $.fn.robotDance = function (options, settings) { return this.each(function () { new robotDance(this, options); }) }; })(window, $);