2

Hello I'm a newcomerin JavaScript language. I started to see some examples of JavaScript code.

and i cant understand the following code segment:

function Employee(name,salary)
 {
  this.name=name;
  this.salary=salary;

  this.paycheck=function()
  {
   var monthly=this.salary/12;
   document.write(this.name+ ": " +monthly);
  };
} 
var emp= new Employee("Fred",10000);
emp.paycheck();

My question: what is the meaning of word this near the property inside class (i.e. ,this.name=name; this.salary=salary; )?

Thank you in advance!

Michael
  • 11,410
  • 43
  • 120
  • 228
  • This is a good explanation of 'this': http://stackoverflow.com/questions/4195970/what-does-this-mean – j08691 Feb 04 '12 at 22:25

5 Answers5

2

The use of this could be explained as meaning "properties that apply to this instance". Since Employee is a constructor function (you create instances of it using the new operator), every instance has its own values for those properties:

var me = new Employee("James", 2000000); //Instance of Employee
console.log(me.name); //Prints James

var you = new Employee("Michael", 2000000); //Another instance
console.log(you.name); //Prints Michael

Note that this also means every instance of Employee has its own copy of the paycheck method. This is not particularly efficient, as a separate copy of the function has to be stored in memory for every instance of the object. You could instead declare the method on the prototype of the Employee object, which would mean the method would be shared between all instances.

James Allardice
  • 156,021
  • 21
  • 318
  • 304
1

In JavaScript this always refers to the “owner” of the function we're executing, or rather, to the object that a function is a method of. When we define our faithful function doSomething() in a page, its owner is the page, or rather, the window object (or global object) of JavaScript. An onclick property, though, is owned by the HTML element it belongs to.

For further reading see these links:

http://www.quirksmode.org/js/this.html http://justin.harmonize.fm/index.php/2009/09/an-introduction-to-javascripts-this/ http://javascriptweblog.wordpress.com/2010/08/30/understanding-javascripts-this/

jacktheripper
  • 12,495
  • 11
  • 51
  • 89
1

this is a reference to the calling context of the function.

Its value will be different based on how a function is invoked.

In your case, because Employee is invoked using new, it's a reference to the new object being constructed.

So when you do...

this.name=name;

...what's happening is that the new object you're creating is being assigned a property name, and its value is being set to whatever was passed to the name parameter of the function.


Here's one way to demonstrate it.

Simplify your function like this...

var foo;

function Employee() {
    foo = this;
}

So now all Employee does is set its this value to the outer variable foo.

Now lets create a new object

var bar = new Employee();

So bar has been assigned whatever Employee returned. If this is the new object constructed, and we assigned it to foo, then foo and bar should have the same object.

foo === bar; // true
1

In this case the Employee function is used a constructor. When the new Employee() syntax is used (as opposed to simply calling Employee()), this refers to an object that you are about to create, which is later assigned to emp variable.

Tomasz Nurkiewicz
  • 311,858
  • 65
  • 665
  • 652
1

it's a definition of properties inside the class. You can read short information about it here http://www.phpied.com/3-ways-to-define-a-javascript-class/

this always refers to the “owner” of the function we're executing

lc0
  • 654
  • 4
  • 9