示例jquery
var jQuery = function() { return jQuery.fn.init(); }; jQuery.fn = jQuery.prototype = { init: function() { this.age = 18; return this; }, name: function() {}, age: 20 }; //原型傳遞 jQuery.fn.init.prototype = jQuery.fn;
由於是引用傳遞,因此不用擔憂循環引用的性能問題。性能
示例this
jQuery.prototype = { funcA: function() { //do something return this; }, funcB: function() { //do something return this; }, //... };
示例prototype
jQuery.extend = jQuery.fn.extend = function() { var src, copyIsArray, copy, name, options, clone, target = arguments[0] || {}, i = 1, length = arguments.length, deep = false; if (typeof target === "boolean") { deep = target; target = arguments[1] || {}; i = 2; } if (typeof target !== "object" && !jQuery.isFunction(target)) { target = {}; } if (length === 1) { target = this; --i; } for (; i < length; i++) { if ((options = arguments[i]) != null) { for (name in options) { src = target[name]; copy = options[name]; if (target === copy) { continue; } if (deep && copy && (jQuery.isPlainObject(copy) || (copyIsArray = jQuery.isArray(copy)))) { if (copyIsArray) { copyIsArray = false; clone = src && jQuery.isArray(src) ? src : []; } else { clone = src && jQuery.isPlainObject(src) ? src : []; } target[name] = jQuery.extend(deep, clone, copy); } else if (copy !== undefined) { target[name] = copy; } } } } return target; };
jQuery.extend 和 jQuery.fn.extend 實際上是指向同一個方法的不一樣引用。
jQuery.extend 對jQuery自己的屬性和方法進行了擴展。
jQuery.fn.extend 對jQuery原型的屬性和方法進行了擴展。插件