第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

用JavaScript實現(xiàn)單例的最簡單/最干凈的方法?

用JavaScript實現(xiàn)單例的最簡單/最干凈的方法?

BIG陽 2019-06-27 16:10:44
用JavaScript實現(xiàn)單例的最簡單/最干凈的方法?在JavaScript中實現(xiàn)單例模式的最簡單/最干凈的方法是什么?
查看完整描述

3 回答

?
千巷貓影

TA貢獻(xiàn)1829條經(jīng)驗 獲得超7個贊

我認(rèn)為最干凈的方法是:

var SingletonFactory = (function(){
    function SingletonClass() {
        //do stuff
    }
    var instance;
    return {
        getInstance: function(){
            if (instance == null) {
                instance = new SingletonClass();
                // Hide the constructor so the returned object can't be new'd...
                instance.constructor = null;
            }
            return instance;
        }
   };})();

之后,您可以將函數(shù)調(diào)用為

var test = SingletonFactory.getInstance();


查看完整回答
反對 回復(fù) 2019-06-27
?
慕容3067478

TA貢獻(xiàn)1773條經(jīng)驗 獲得超3個贊

我不同意將模塊模式用作單例模式的替代品。我經(jīng)??吹皆谕耆珱]有必要的地方使用和濫用單例,我確信模塊模式填補(bǔ)了許多空白,否則程序員就會使用單例,但是模塊模式是單身人士。

模塊模式:

var foo = (function () {
    "use strict";
    function aPrivateFunction() {}
    return { aPublicFunction: function () {...}, ... };}());

在模塊模式中初始化的所有內(nèi)容都發(fā)生在Foo被宣布。此外,模塊模式可以用于初始化構(gòu)造函數(shù),然后可以多次實例化構(gòu)造函數(shù)。雖然模塊模式是許多作業(yè)的正確工具,但它并不等同于單例。

單例模式:

短形

var Foo = function () {
    "use strict";
    if (Foo._instance) {
        //this allows the constructor to be called multiple times
        //and refer to the same instance. Another option is to
        //throw an error.
        return Foo._instance;
    }
    Foo._instance = this;
    //Foo initialization code};Foo.getInstance = function () {
    "use strict";
    return Foo._instance || new Foo();}

長格式,使用模塊模式

var Foo = (function () {
    "use strict";
    var instance; //prevent modification of "instance" variable
    function Singleton() {
        if (instance) {
            return instance;
        }
        instance = this;
        //Singleton initialization code
    }
    //instance accessor
    Singleton.getInstance = function () {
        return instance || new Singleton();
    }
    return Singleton;}());

在我提供的Singleton模式的兩個版本中,構(gòu)造函數(shù)本身都可以用作訪問器:

var a,
    b;a = new Foo(); //constructor initialization happens hereb = new Foo();console.log(a === b); //true

如果不習(xí)慣以這種方式使用構(gòu)造函數(shù),則可以在if (instance)語句,并堅持使用長表單:

var a,
    b;a = Foo.getInstance(); //constructor initialization happens hereb = Foo.getInstance();console.log(a === b); //true

我還應(yīng)該指出,單例模式與隱式構(gòu)造函數(shù)模式非常匹配:

function Foo() {
    if (Foo._instance) {
        return Foo._instance;
    }
    //if the function wasn't called as a constructor,
    //call it as a constructor and return the result
    if (!(this instanceof Foo)) {
        return new Foo();
    }
    Foo._instance = this;}var f = new Foo(); //calls Foo as a constructor-or-var f = Foo(); //also calls Foo as a constructor


查看完整回答
反對 回復(fù) 2019-06-27
  • 3 回答
  • 0 關(guān)注
  • 474 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號