- 博客(4)
- 收藏
- 关注
原创 闭包也就这玩意
闭包也叫closure这东西听起来挺玄乎,《js犀牛书》上搞了一计数器作为例子,如下: function counter() { var n = 0; return { count: function() { return n++; }, reset: function() { n = 0; } }; } var c =...
2011-07-25 15:34:38 103
call和apply到底有啥用
为什么有这两个函数呢?们到底可以做什么? mozilla官方的文档让我看到了一点端倪: 1、call函数可以让你从已有的对象中继承一个方法,而不必为新对象写一个新的方法 With call, you can write a method once and then inherit it in another object, without having to rew...
2011-07-24 20:49:26 237
原创 for/in不适合array遍历的情形
1、for/in将遍历从原型中继承的属性。 因此遍历数组的时候有诸多不便,需要通过以下方式保证属性是属于当前数组而不是继承来的。 for( var i in a) { if (!a.hasOwnProperty(i)) continue; //循环语句 } 2、for/in遍历对象的时候顺序是不定的。 数组中可能有Number,String,O...
2011-07-23 13:39:31 499
原创 javascript悬挂
先看一段代码: var scope = "global"; function f() { console.log(scope); var scope = "local"; console.log(scope); } 这段代码的输出结果是: undefined local js犀牛书上的解释是,与全局变量同名的局部变量在整个函数内部覆盖全局变量。简而言之,所...
2011-07-21 19:22:58 226
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人