<script language="javascript">
function person(name){
this.name = name;
}
person.prototype.say = function(){alert("hello, i'm " + this.name);}function employee(name, salary){
person.call(this, name);
this.salary = salary;
}
employee.prototype = new person(); //为什么要写这句?employee.prototype.showMoney = function(){alert(this.name + "'s salary is " + this.salary);}alert(employee.prototype == person.prototype);
alert(employee.prototype == person);
</script>上面不是有person.call(this, name)这句吗?这句不久可以直接从上面的函数继承的吗?为什么还要加employee.prototype = new person()这句?这句和person.call(this, name)有什么不同?
function person(name){
this.name = name;
}
person.prototype.say = function(){alert("hello, i'm " + this.name);}function employee(name, salary){
person.call(this, name);
this.salary = salary;
}
employee.prototype = new person(); //为什么要写这句?employee.prototype.showMoney = function(){alert(this.name + "'s salary is " + this.salary);}alert(employee.prototype == person.prototype);
alert(employee.prototype == person);
</script>上面不是有person.call(this, name)这句吗?这句不久可以直接从上面的函数继承的吗?为什么还要加employee.prototype = new person()这句?这句和person.call(this, name)有什么不同?
person.call(this, name); 继承类的实例的属性和方法person.prototype.name2 = 'bbb'
employee.prototype = new person(); 继承类的原型的属性和方法
一个是在本身的属性上如新生成的对象 var a = new employee('haha')a可以看成
{
name:'haha',
__proto__ :{
name:undefined
}
}一个例子
Object.prototype.name="hehea" //为了方便查看 Object的原形上也加了name属性
function person(name){
this.name = name;
}
person.prototype.say = function(){alert("hello, i'm " + this.name);}function employee(name, salary){
person.call(this, name);
this.salary = salary;
}
employee.prototype = new person(); //为什么要写这句?employee.prototype.showMoney = function(){alert(this.name + "'s salary is " + this.salary);}
var a = new employee('haha')alert(a.name);
delete a.name;
alert(a.name)
delete employee.prototype.name
alert(a.name)
应该不是这样的吧?call()也可以继承方法,prototype也可以继承属性啊
person.call(this, name); 继承类的实例的属性和方法person.prototype.name2 = 'bbb'
employee.prototype = new person(); 继承类的原型的属性和方法