Why is it that just by using call you’re adding properties to the Employee instance named jc?
I’m wondering why jc.hasOwnProperty('firstName'); results to true.
I didn’t even do prototype inheritance yet. This is all the code, no more no less:
I understand that call just changes the this while passing in arguments, but to add properties? I don’t know what’s going on…
function Person(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}
function Employee(firstName, lastName, position){
Person.call(this,firstName,lastName);
this.position = position;
}
var jc = new Employee('JC','Viray','Developer');
jc.hasOwnProperty('firstName'); //true
UPDATE
I get it now.. the solution to my problem is:
stop thinking in C#/Java mentality.. I lost track that Person is still a FUNCTION despite it being a “constructor type function”… -_- once a function, still a function..
Because of this line in the
Personfunction:When you say
You’re actually calling the
Personfunction, and tellingPersonto set itsthisvalue to what’s provided in the first argument—in your case, the Employee object you’re in the process of building. The subsequent arguments tocallthen get passed along as regular parameters.As a result,
Persongets called withthisset to be your currentEmployeeobject, and picks up afirstNameandlastNameproperty.Or consider an alternate example.