摘要:
1 Math.guid = function() {2 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {3 var r = Math.random() * 16 | 0,4 v = c == 'x' ? r : (r & 0x3 | 0x8);5 return v.toString(16);6 }).toUpperCase();7 }; 阅读全文
摘要:
1 var PubSub = { 2 subscribe: function(ev, callback) { 3 //创建 _callbacks 对象,除非它已经存在了 4 var calls = this._callbacks || (this._callbacks = {}); 5 //针对给定的事件 ev 创建一个数组,除非这个数组已经存在 6 //然后将回调函数追加到这个数组中 7 (this._callbacks[ev] || (this._callbacks[ev] = [])).push(c... 阅读全文
摘要:
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> 3 <head> 4 <meta http-equiv="Content-Type" co 阅读全文
摘要:
1 var proxy = function(func, thisObject) { 2 return (function() { 3 return func.apply(thisObject, arguments); 4 }); 5 }; 6 7 //example 8 var clicky = { 9 wasClicked: function() {},10 addListeners: function() {11 var self = this;12 $('.clicky'... 阅读全文
摘要:
1 var clicky = { 2 wasClicked: function() {}, 3 addListeners: function() { 4 var self = this; 5 $('.clicky').click(function() { 6 self.wasClicked(); 7 }); 8 } 9 };10 clicky.addListeners();11 12 13 //使用apply14 var proxy = function(func, thisObject) {15... 阅读全文
摘要:
1 var Animal = function() {}; 2 3 Animal.prototype.breath = function() { 4 console.log('breath'); 5 }; 6 7 var Dog = function() {}; 8 9 //Dog继承了Animal10 Dog.prototype = new Animal;11 12 Dog.prototype.wag = function() {13 console.log('wag tail');14 };15 16 var dog = new Dog;17 dog.wag 阅读全文
摘要:
1if(typeofObject.beget!=='function'){2Object.beget=function(o){3varF=function(){};4F.prototype=o;5returnnewF();6};7}89vara={10member:true11};12varb=Object.beget(a);13vart=a.hasOwnProperty('member');14varu=b.hasOwnProperty('member');15varv=b.member;1617console.log(t);18console 阅读全文
摘要:
1 Function.prototype.method = function(name, func) { 2 if (!this.prototype[name]) { 3 this.prototype[name] = func; 4 } 5 }; 6 7 Function.method('bind', function(that) { 8 //返回一个函数,调用这个函数就像它是那个对象的方法一样。 9 var method = this,10 slice = Array.prototype.slice,11 a... 阅读全文
摘要:
1 //数组排序 2 var n = [15, 99, 23, 42, 4, 8, 16]; 3 n.sort(function(a, b) { 4 return a - b 5 }); 6 console.log(n); 7 8 //字符串排序 9 var m = ['aa', 'bb', 'a', 4, 8, 15, 16, 23, 42];10 m.sort(function(a, b) {11 if (a === b) {12 return 0;13 }14 if (typeof a === typeof b) {15 ... 阅读全文
摘要:
function parseUrl(url) { var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/; var result = parse_url.exec(url); var names = ['url', 'scheme', 'slash', 'host', 'port', 'path', 'query' 阅读全文