lz的理解语法太乱。不太明白要表达什么意思 至于那个演示是执行一个方法,那个方法返回一个对象,对象里有2个方法,并且规定了count 的作用域,当执行的时候各count 之间互不影响。而lz的理解大概是想用面向对象的new来取代。虽然是可行的,但闭包只是js里的一个特性,如果只是学习的话就只要了解就行了,什么情况下方便就用。 就想for和each
  if else和switch case

解决方案 »

  1.   

    上面写的Counter是一种类似于面向对象的类。
    下面的Counter实际上还是一个函数。只是包含了两个内函数,共享count 变量。
    两部分都用到了闭包。
    上面的return把两个函数进行封装,使其成为类Counter的成员函数。
    所以才可以使用如下语法
    var foo =Counter(4);//创建一个Counter对象foo
    foo.increment();//调用对象foo的increment方法
    foo.get();// 5下面的写法显然是无法执行的。
      

  2.   

    通过函数创建对象 具体什么语法啊? 我看其他的一个教程里怎么有new 关键字呢?
    var Person = function(n,a,s) {
    var name = n;
    var sex = s;
    var age = a;
    return {
    get_Name : function() {
    return name;
    }
    ,
    set_Name : function(v) {
    name = v;
    }

    get_Sex : function() {
    return sex;
    }
    ,
    set_Sex : function(v) {
    sex = v;
    }
    ,
    get_Age : function() {
    return age;
    }
    ,
    set_Age : function(v) {
    age = v;
    }
    };

    var p = new Person("小王", 19, "男");
    alert(p.get_Name() + ", " + p.get_Age() + ", " + p.get_Sex());
      

  3.   

    var p = Person("小王", 19, "男");
    alert(p.get_Name() + ", " + p.get_Age() + ", " + p.get_Sex());是一样的
      

  4.   

    function Counter(start){ 
            var count = start;
      return{
      increment:function(){
      count++;
      },
      get:function(){
      return count;
      }
      }
      }
      var foo =Counter(4);
      foo.increment();
      foo.get();// 5
    问下我第一个例子中foo对象中 是否含有字段count  按二楼所说   2方法互不影响是什么意思?
      

  5.   

    我只想说我晕!  return{
      increment:function(){
      count++;
      },
      get:function(){
      return count;
      }
      }这一段代码表示返回一个对象。这个对象里面有两个方法一个是increment,另一个是get。这两个方法都是在函数Counter里面定义的所以它们都是闭包。如果楼主不明白,楼主应该要自觉的看一下JS里面对象的创建和闭包的定义。因为是闭包所以increment和get它们都保存了对存有Counter变量对象的引用,所以它们都可以访问count。
      

  6.   

    Counter 函数相当于一个工厂方法,这个工厂方法产生一个对象并作为结果返回。
    注意,其返回的结果是一个对象字面量,不需要new,可以直接用。
    需要new的是用函数定义的类,建议楼主参考下JS里面向对象编程的相关知识。
      

  7.   

    其实,Counter和下面的写法是相同的。
    function Counter(start)
    {
    var count = start;
    var result = // result是一个对象字面量
            {
                increment : function ()
                {
                    count++;
                      
                },
                get : function ()
                {
                    return count;
                      
                }
                  
            };
        return result;
      
    }
      var foo = Counter(4);
      foo.increment();
      foo.get(); // 5要Counter成为一个类,可以用下面的实现;
    Counter.prototype = 
    {
        increment : function ()
        {
            this.count++;
              
        },
        get : function ()
        {
            return this.count;
              
        }
    }然后就可以 new 了:
       var foo = new Counter(4);
      foo.increment();
      foo.get(); // 5
      

  8.   

    第二段代码少了一些东西,下面是对的:function Counter(start)
    {
    this.count = start;
      
    }Counter.prototype = 
    {
        increment : function ()
        {
            this.count++;
              
        },
        get : function ()
        {
            return this.count;
              
        }
    }
      var foo = new Counter(4);
      foo.increment();
      foo.get(); // 5
      

  9.   

    按你的理解(类似后台的类)应该是这样    function Counter(start) {
                this._count = start;
            }
            Counter.prototype.increment = function () {
                this._count ++;
            }
            Counter.prototype.get = function () {
                return this._count ;
            }p.s : prototype 不要直接覆盖,因为constructor有时会用到 .