`
jingwei.yang
  • 浏览: 3087 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论
文章列表
闭包也叫closure这东西听起来挺玄乎,《js犀牛书》上搞了一计数器作为例子,如下:   function counter() { var n = 0; return { count: function() { return n++; }, reset: function() { n = 0; } }; } var c = counter(), d = counter(); // Create two counters c.count() // => 0 ...
     为什么有这两个函数呢?们到底可以做什么? mozilla官方的文档让我看到了一点端倪: 1、call函数可以让你从已有的对象中继承一个方法,而不必为新对象写一个新的方法 With call, you can write a method once and then inherit it in another object, without having to rewrite the method for the new object. 文档中的例子说明了这一点: function Product(name, value){ this.name ...
1、for/in将遍历从原型中继承的属性。 因此遍历数组的时候有诸多不便,需要通过以下方式保证属性是属于当前数组而不是继承来的。 for( var i in a) { if (!a.hasOwnProperty(i)) continue; //循环语句 }   2、for/in遍历对象的时候顺序是不定的。 数组中可能有Number,String,Object等各种各样的对象,而他们的定义的时候跟index无关,所以如果数组遍历需要按照顺序,则for/in不适合。   总之,数组的遍历最好用for循环进行。
先看一段代码:   var scope = "global"; function f() { console.log(scope); var scope = "local"; console.log(scope); } 这段代码的输出结果是: undefined local  js犀牛书上的解释是,与全局变量同名的局部变量在整个函数内部覆盖全局变量。简而言之,所有函数体内的变量声明(注意,仅仅是声明)都将被提到函数体开头进行。上述代码相当于以下代码的运行结果:   var scope = "global&q ...
Global site tag (gtag.js) - Google Analytics