Kamran Ahmed
Kamran Ahmed

Reputation: 12438

Accessing the object properties via `this` vs `objectName`

I have been using JS Modular pattern throughout the application. The modules look like the following:

var moduleName = {    

    prop1 : 'value1',
    prop2 : 'value2',

    fun1Name : function () {
       // body of funName
       moduleName.fun2Name(); // notice the way I am calling the function using moduleName
                              // Didn't use this.fun2Name()
    },

    fun2Name : function () {
       // body of functName
    }

};

And inside the modules, I have been accessing the functions using moduleName.functionName() which may also be accessed (as we all know) using this.functionName(). Now I am refactoring the code and I was just curious to know that:

  1. Is there any reason that I should change moduleName.functionName() to this.functionName() wherever possible?
  2. Are there any performance issues associated with both the ways of calling the module functions?
  3. What's the best way to call the module functions inside the module itself?

Upvotes: 0

Views: 38

Answers (1)

Quentin
Quentin

Reputation: 943216

  1. It makes your code reusable between different but similar objects (typically instances of the same constructor function)
  2. No
  3. That's subjective

Upvotes: 1

Related Questions