this in constructor functions

suggest change

When using a function as a constructor, it has a special this binding, which refers to the newly created object:

function Cat(name) {
  this.name = name;
  this.sound = "Meow";
}

var cat = new Cat("Tom"); // is a Cat object
cat.sound; // Returns "Meow"

var cat2 = Cat("Tom"); // is undefined -- function got executed in global context
window.name; // "Tom"
cat2.name; // error! cannot access property of undefined

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents