[removed] Do I need to put this.var for every variable in an object?

前端 未结 6 1325
太阳男子
太阳男子 2020-11-21 05:06

In C++, the language I\'m most comfortable with, usually one declares an object like this:

class foo
{
public:
    int bar;
    int getBar() { return bar; }
         


        
6条回答
  •  别跟我提以往
    2020-11-21 05:35

    To get closer to OOP in JavaScript, you might want to take a look into a Module design pattern (for instance, described here).

    Based on the closure effect, this pattern allows emulating private properties in your objects.

    With 'private' properties you can reference them directly by its identifier (i.e., no this keyword as in constructors).

    But anyway, closures and design patterns in JS - an advanced topic. So, get familiar with basics (also explained in the book mentioned before).

提交回复
热议问题