0

I am really struggling to understand how does this works in constructor function for object creation in Javascript environment.

below is the code snippet:

function Employee() {
    basicSalary = 12000;
    overtime = 10;
    rate = 200;
    this.getWage = function () {
        return basicSalary+(overtime*rate);
    }
}
let emp = new Employee();
console.log(emp.basicSalary);//undefined
console.log(emp.getWage());//14000

in above code I am getting undefined for basicSalary variable. So in order to get basicSalary value I prefix it with this inside function like:

this.basicSalary = 12000;

But, in doing so I am getting error in accessing getWage() method. It says:

Uncaught ReferenceError: basicSalary is not defined
    at Employee.getWage (index.js:6)
    at index.js:11

However, if I prefix this to the basicSalary in returning statement inside getWage() function, I am able to access both basicSalary variable and getWage() method.

Also If I replace all my code to factory function i.e. Object literal type like here:

function employee(){
    return {
        basicSalary : 12000,
        overtime : 10,
        rate : 200,
        getWage:function(){
            return this.basicSalary+(this.overtime*this.rate); // accessing values through `this`
        }
    };
}

const e2 = employee();
console.log(e2.basicSalary);
console.log(e2.getWage());

Now I am easily able to get both basicSalary and getWage() method. Only thing that I needed to do was put this in return statement inside getWage() method.

in both these approaches this works differently. I am confused. Please share some knowledge on how to use this in Javascript.

4
  • I can help you with the article I came across when I was trying to figure out how 'this' works in JavaScript. javascriptissexy.com/… Commented Feb 22, 2020 at 7:49
  • ok. I am seeing it. thanks for sharing Commented Feb 22, 2020 at 8:00
  • 1
    “In both this approaches this works differently ” Why do you think that? What do you perceive as different? The biggest difference between basicSalary = ... and this.basicSalary = ... is that the first creates a variable and the second creates an object property. You cannot refer to an object property as if it was a variable (your first attempt to fix it). The last example is almost the same as the previous one, except you are creating the object explicitly vs having new create it for you. Commented Feb 22, 2020 at 8:57
  • @FelixKling Ok. Got it. Commented Feb 22, 2020 at 11:18

2 Answers 2

1

In the first case, you are either (implicitly) declaring new local variables (scoped to the execution of the function), or referencing global variables that exist in a higher scope.

They are not bound to the object returned with a new.

In the second case you are returning an object literal, so they are bound to it, obviously.

Amit’s code is good. You can also do this:

function Employee() {
    this.basicSalary = 12000;
    this.overtime = 10;
    this.rate = 200;
    this.getWage = () =>  this.basicSalary + (this.overtime*this.rate)
}

If you have arrow functions though, you also have ES6 classes. That may be a better match.

Sign up to request clarification or add additional context in comments.

1 Comment

Since I can see how your answer could be misinterpreted: This example works exactly the same with a normal function. The arrow function is not necessary.
0

Once you read the article I commented you can try this.

function Employee() {
    this.basicSalary = 12000;
    this.overtime = 10;
    this.rate = 200;
    this.getWage = function () {
        return this.basicSalary + (this.overtime*this.rate);
    }
}
let emp = new Employee();
console.log(emp.basicSalary);//12000
console.log(emp.getWage());//14000

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.