can someone explain what's the difference between this:
<script>
function Employee() {
this.name = "serj";
}
function Manager() {
Employee.call(this);
this.dept = "general";
}
var jim = new Manager;
console.log(jim.name); //serj
</script>
And this:
<script>
function Employee() {
this.name = "serj";
}
Manager.prototype = Object.create(Employee.prototype);
function Manager() {
Employee.call(this);
this.dept = "general";
}
var jim = new Manager;
console.log(jim.name); //serj
</script>
So the question is why should I create a prototype?If manager calls employee and all works fine?
答案 0 :(得分:0)
Calling the constructor function for Employee
will run all the code that is inside that function. It won't perform all the side effects (i.e. creating a new object, assigning it to this
and making it an instance of Employee
… and you wouldn't want it to because you want it to be an instance of Manager
).