Both of them are actually incorrect. But more on that later.
The second one is incorrect because objects don't have prototype
property. Only functions have a such a property.
In the second example, this
is an object, so this.prototype
is undefined
. In the first example you are setting Person.prototype
and Person
is a function, so all is "good".
Why is the first example still wrong? Because you usually don't have a reason to extend the prototype object inside the constructor. The constructor should only contain instance specific code and the prototype object should hold properties which are shared by all instances.
So your above example is correctly written as:
function Person(name, age, job){
this.name = name;
this.age = age;
this.job = job;
}
Person.prototype.sayName = function(){
alert(this.name);
};