自动执行功能与对象的关闭 [英] Closures in auto executing functions vs objects

查看:93
本文介绍了自动执行功能与对象的关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有以下:

var foo = (function(){
    var bar = 0;
    return {
       getBar: function(){
           return bar;
       },
       addOne: function(){
           bar++;
       },
       addRandom: function(rand){
           bar += rand;
       }
    }
})();

我有以下:

var foo2 = function(){
    var bar = 0;
    this.getBar = function(){
           return bar;
       };
    this.addOne = function(){
           bar++;
       };
    this.addRandom = function(rand){
           bar += rand;
       }
};

是执行函数的唯一区别a new

Is the only difference in executing the functions a new?

alert(foo.getBar()); //0
foo.addOne();
foo.addRandom(32);
alert(foo.getBar()); //33

var foo2_obj = new foo2;
alert(foo2_obj.getBar());//0
foo2_obj.addOne();
foo2_obj.addRandom(32);
alert(foo2_obj.getBar());//33

完全相同的事情。

那么长期的区别是什么?

So what is the difference in the long run?

其他不能?

上面的示例演示: http://jsfiddle.net / maniator / YtBpe /

推荐答案

在第一个中,你只能创建一次对象,一个你可以创建任意多的对象,你喜欢。 I.E.第一个实际上是一个单例。

In the first one you can only create the object once, while with the second one you can create as many objects as you like. I.E. the first one is effectively a singleton.

请注意,闭包不适合第二个。每次你实例化它,你再次创建的功能,浪费了大量的内存。原型对象旨在解决这个问题,您可以在函数范围之外创建函数,并且不会创建意外关闭。

Note that closures are not ok for the second one. Every time you instantiate it you are creating the functions all over again and waste a ton of memory. The prototype object is intended to counter this, where you can create the functions once outside a function scope and no accidental closures are created.

function foo2(){
    this._bar = 0;
}

foo2.prototype = {

    constructor: foo2,

    getBar: function(){
        return this._bar;
    },

    addOne: function(){
        this._bar++;
    },

    addRandom:function(rand){
        this._bar += rand;
    }

};

然后:

var a = new foo2, b = new foo2, c = new foo2;

创建三个具有自己 _bar 但共享相同的功能。

Creates three instances which have their own _bar but share the same functionality.

jsperf

您可以将所有这些都比较到PHP,一些代码甚至不会运行,但它在原则上是等效

You can "compare" all of this to PHP, some of the code won't even run but it's "equivalent" in principle:

var foo = (function(){
    var bar = 0;
    return {
       getBar: function(){
           return bar;
       },
       addOne: function(){
           bar++;
       },
       addRandom: function(rand){
           bar += rand;
       }
    }
})();

在PHP中大致相当于:

is roughly "equivalent" to this in PHP:

$foo = new stdClass;

$foo->bar = 0;

$foo->getBar = function(){
    return $this->bar;
};

$foo->addOne = function(){
    $this->bar++;
}

$foo->addRandom = function($rand){
    $this->bar += $rand;
}







var foo2 = function(){
    var bar = 0;
    this.getBar = function(){
        return bar;
    };
    this.addOne = function(){
        bar++;
    };
    this.addRandom = function(rand){
        bar += rand;
    }
};

在PHP中大致等同于此:

Is roughly "equivalent" to this in PHP:

Class foo2 {


    public function __construct(){
    $bar = 0;

        $this->getBar = function(){
            return $bar;
        };
        $this->addOne = function(){
            $bar++;
        };
        $this->addRandom = function($rand){
            $bar += rand;
        };


    }

}







function foo2(){
    this._bar = 0;
}

foo2.prototype = {

    constructor: foo2,

    getBar: function(){
        return this._bar;
    },

    addOne: function(){
        this._bar++;
    },

    addRandom:function(rand){
        this._bar += rand;
    }

};

在PHP中大致等同于此:

Is roughly "equivalent" to this in PHP:

Class foo2 {

    public $_bar;

    public function __construct(){
        $this->_bar = 0;    
    }

    public function getBar(){
        return $this->_bar;    
    }

    public function addOne(){
        $this->_bar++
    }

    public function addRandom($rand){
        $this->_bar += $rand;
    }

}

...一个接近上述三个例子中的OOP

...and is the only one that is close to OOP in the three above examples

这篇关于自动执行功能与对象的关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆