If I define a property to prototype appears in the constructor of object, why?

Posted by Eduard Florinescu on Programmers See other posts from Programmers or by Eduard Florinescu
Published on 2012-09-10T20:48:39Z Indexed on 2012/09/10 21:49 UTC
Read the original article Hit count: 361

I took the example from this question modified a bit: What is the point of the prototype method?

function employee(name,jobtitle,born)
{
this.name=name;
this.jobtitle=jobtitle;
this.born=born;
this.status="single"
}
employee.prototype.salary=10000000;
var fred=new employee("Fred Flintstone","Caveman",1970);
console.log(fred.salary);
fred.salary=20000;
console.log(fred.salary)

And the output in console is this:

enter image description here

What is the difference salary is in constructor but I still can access it with fred.salary, how can I see if is in constructor from code, status is still employee property how can I tell for example if name is the one of employee or has been touch by initialization?

Why is salary in constructor, when name,jobtitle,born where "touched" by employee("Fred Flintstone","Caveman",1970); «constructor»?

© Programmers or respective owner

Related posts about object-oriented

Related posts about JavaScript