javascript - Is it good to use object literals to organize code?
伊谢尔伦
伊谢尔伦 2017-06-12 09:28:53
0
3
654
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() {
        ……
    }
})();

Which one of these two is better? The object method looks very clear, but there are many disadvantages in using it. For example, when looking for methods and variables, you have to put this in front of them. Will this increase unnecessary performance consumption?

Writing it as an object will facilitate expansion, etc. Because inheritance and polymorphism can be carried out in an object-oriented manner. If the program iteration encounters logic that is the same or similar to the logic in the object in the future, it will be much more convenient~

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(3)
巴扎黑

Consider using ES6+Babel. Using classes can also provide better OO

洪涛

js has deviated far from the author's original intention. The mainstream keeps twisting it towards OO, and another niche school wants to train it into functional style. Back to the question, I can see that the questioner is leaning towards OO, so just follow the ES6 and ES7 routines to make it better, and there will be no difference in performance.

我想大声告诉你

To understand it simply, it doesn’t matter. In fact, if you don’t pollute the external environment, you can do it no matter what you do.
One more thing, don’t let your friend who takes over your code hate you...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template