<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8" /> <title></title> </head> <body> <a href="">1</a> <a href="">2</a> <script> (function(window, document, undefined) { var MYAD = function(selector) { return new MYAD.fn.init(selector); }; MYAD.fn = MYAD.prototype = { constructor: MYAD, init: function(selector) { this.selector = this[0] = selector; return this; } }; MYAD.fn.init.prototype = MYAD.fn; MYAD.extend = MYAD.fn.extend = function() { var target = arguments[0] || {}, i = 1, length = arguments.length, options, name, src, copy; if (length === i) { target = this; i--; } for (; i < length; i++) { if ((options = arguments[i]) != null) { for (name in options) { src = target[name]; copy = options[name]; target[name] = copy; } } } return target; }; MYAD.extend({ sayHi: function() { console.log('hi'); }, sayHello: function() { console.log('hello'); } }); MYAD.fn.extend({ setColor: function() { var sel = this[0]; var len = sel.length; if (len > 1) { while (len--) { sel[len].style.background = '#f00'; } } else { sel.style.color = '#f00'; } } }); window.MYAD = MYAD; })(this, this.document); MYAD.sayHi(); MYAD.sayHello(); MYAD(document.getElementsByTagName('a')).setColor(); </script> </body> </html>
http://jquery.com/