随笔分类 -  js/jq

上一页 1 2 3 4 5 6 7 8 ··· 10 下一页
摘要: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 阅读全文
posted @ 2012-07-23 13:18 小猩猩君 阅读(122) 评论(0) 推荐(0) 编辑
摘要: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... 阅读全文
posted @ 2012-07-18 12:37 小猩猩君 阅读(302) 评论(0) 推荐(0) 编辑
摘要: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 ... 阅读全文
posted @ 2012-07-17 13:35 小猩猩君 阅读(241) 评论(0) 推荐(0) 编辑
摘要: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' 阅读全文
posted @ 2012-07-16 14:19 小猩猩君 阅读(3074) 评论(0) 推荐(0) 编辑
摘要:regexp.exechttp://www.w3school.com.cn/js/jsref_exec_regexp.aspregexp.testhttp://www.w3school.com.cn/js/jsref_test_regexp.aspstring.matchhttp://www.w3school.com.cn/js/jsref_match.aspstring.replacehttp://www.w3school.com.cn/js/jsref_replace.aspstring.searchhttp://www.w3school.com.cn/js/jsref_search.as 阅读全文
posted @ 2012-07-16 13:46 小猩猩君 阅读(129) 评论(0) 推荐(0) 编辑
摘要:判断是否为数组1 var is_array = function(value) {2 return value && typeof value === 'object' && typeof value.length === 'number' && typeof value.splice === 'function' && !(value.propertyIsEnumerable('length'));3 };4 5 //使用方法6 var arrayValue = [ 阅读全文
posted @ 2012-07-15 16:51 小猩猩君 阅读(174) 评论(0) 推荐(0) 编辑
摘要:1 var mammal = function(spec) { 2 var that = {}; 3 4 that.get_name = function() { 5 return spec.name; 6 }; 7 8 that.says = function() { 9 return spec.saying || '';10 };11 return that;12 };13 14 var myMammal = mammal({15 ... 阅读全文
posted @ 2012-07-13 14:44 小猩猩君 阅读(159) 评论(0) 推荐(0) 编辑
摘要:1 var mammal = function(spec) { 2 var that = {}; 3 4 that.saying = function() { 5 return spec.saying; 6 }; 7 8 that.get_name = function() { 9 return spec.name;10 };11 12 that.says = function() {13 return this.sayi... 阅读全文
posted @ 2012-07-13 10:26 小猩猩君 阅读(159) 评论(0) 推荐(0) 编辑
摘要:1 if (typeof Object.beget !== 'function') { 2 Object.beget = function(o) { 3 var F = function() {}; 4 F.prototype = o; 5 return new F(); 6 }; 7 } 8 9 var myMammal = {10 name: 'Herb the Mammal',11 get_name: function() {12 return this.name;13 },14 ... 阅读全文
posted @ 2012-07-12 22:15 小猩猩君 阅读(268) 评论(0) 推荐(0) 编辑
摘要:Object.create方法创建一个使用原对象作为其原型的新对象。1 if (typeof Object.create !== 'function') {2 Object.create = function (o) {3 function F() {}4 F.prototype = o;5 return new F();6 };7 }8 newObject = Object.create(oldObject); 阅读全文
posted @ 2012-07-12 21:49 小猩猩君 阅读(267) 评论(0) 推荐(0) 编辑
摘要:1 var memoizer = function(memo, fundamental) { 2 var shell = function(n) { 3 var result = memo[n]; 4 if (typeof result !== 'number') { 5 result = fundamental(shell, n); 6 memo[n] = result; 7 } 8 ... 阅读全文
posted @ 2012-07-12 12:49 小猩猩君 阅读(186) 评论(0) 推荐(0) 编辑
摘要:var memoizer = function(memo, fundamental) { var shell = function(n) { var result = memo[n]; if (typeof result !== 'number') { result = fundamental(shell, n); memo[n] = result; } return result; ... 阅读全文
posted @ 2012-07-12 12:48 小猩猩君 阅读(215) 评论(0) 推荐(0) 编辑
摘要:1 Function.prototype.method = function(name, func) { 2 if (!this.prototype[name]) { 3 this.prototype[name] = func; 4 } 5 }; 6 7 Function.method('curry', function() { 8 var slice = Array.prototype.slice, 9 args = slice.apply(arguments),10 that = this;11 retur... 阅读全文
posted @ 2012-07-12 12:30 小猩猩君 阅读(128) 评论(0) 推荐(0) 编辑
摘要:1 var serial_maker = function() { 2 //返回一个用来产生唯一字符串的对象。 3 //唯一字符串由两部分组成:前缀+序列号。 4 //该对象包含一个设置前缀的方法,一个设置序列号的方法,和一个产生唯一字符串的 gensym 方法。 5 var prefix = ''; 6 var seq = 0; 7 return { 8 set_prefix: function(p) { 9 prefix = String... 阅读全文
posted @ 2012-07-11 13:33 小猩猩君 阅读(207) 评论(0) 推荐(0) 编辑
摘要:1 Function.prototype.method = function(name, func) {2 if (!this.prototype[name]) {3 this.prototype[name] = func;4 }5 };具体使用和YUI类似 阅读全文
posted @ 2012-07-11 13:10 小猩猩君 阅读(235) 评论(0) 推荐(0) 编辑
摘要:1 Function.prototype.method = function(name, func) { 2 if (!this.prototype[name]) { 3 this.prototype[name] = func; 4 } 5 }; 6 7 String.method('deentityify', function() { 8 //字符实体表。它映射字符实体的名字到对应的字符。 9 var entity = {10 quot: '"',11 lt: '<',12 gt: '>'13 };1415 阅读全文
posted @ 2012-07-11 13:08 小猩猩君 阅读(3438) 评论(0) 推荐(0) 编辑
摘要: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 阅读全文
posted @ 2012-07-10 14:28 小猩猩君 阅读(246) 评论(0) 推荐(0) 编辑
摘要:1 var fade = function(node) { 2 var level = 1; 3 var step = function() { 4 var hex = level.toString(); 5 node.style.backgroundColor = '#FFFF' + hex + hex; 6 if (level < 15) { 7 level += 1; 8 setT... 阅读全文
posted @ 2012-07-10 14:09 小猩猩君 阅读(194) 评论(0) 推荐(0) 编辑
摘要:1 //定义 walk_the_DOM 函数,它从某个给定的节点开始,按HTML源码中的顺序访问该树的每个节点 2 //它会调用一个函数,并依次传递每个节点给它。walk_the_DOM 调用自身去处理每一个子节点。 3 var walk_the_DOM = function walk(node, func) { 4 func(node); 5 node = node.firstChild; 6 while (node) { 7 walk(node, func); 8 node = node.ne... 阅读全文
posted @ 2012-07-09 22:17 小猩猩君 阅读(438) 评论(0) 推荐(0) 编辑
摘要:1 var hanoi = function(disc, src, aux, dst) {2 if (disc > 0) {3 hanoi(disc - 1, src, dst, aux);4 document.writeln('Move disc ' + disc + ' from ' + src + ' to ' + dst + '<br/>');5 hanoi(disc - 1, aux, src, dst);6 }7 };8 hanoi(3, 'Src', 'Aux', & 阅读全文
posted @ 2012-07-09 14:06 小猩猩君 阅读(157) 评论(0) 推荐(0) 编辑

上一页 1 2 3 4 5 6 7 8 ··· 10 下一页