用对象字面量方式来组织代码好吗?

var test = {

    globalVariable: 'abc',

    

    init: function () {

        this.method();

        this.method0();

    },

    

    method: function () {

        ……

    },

    

    method0: function () {

        ……

    }

};

Or

(function () {

    var globalVariable = 'abc';

    

    // init

    method();

    method0();

    

    function method() {

        ……

    }

    function method0() {

        ……

    }

})();

这两种哪种更好一点?对象方式的看起来很清晰 但用起来也有很多弊端 比如找方法、变量前边都得带上 this 这样会不会增加了不必要的性能消耗?

写成对象会不会方便了扩展等 因为可以已面向对象的方式进行继承、多态 万一以后程序迭代遇到逻辑和对象里的逻辑一样或相似 就会方便很多~


白板的微信
浏览 467回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript