function Person(name)
{
this.name = name;
this.show = function() {
alert("hi");
}
};
Person.prototype.company = "Microsoft"; //原型的属性
Person.prototype.SayHello = function() //原型的方法
{
alert("Hello, I'm " + this.name + " of " + this.company);
};
function BillGates (name){
//Person.call(this,name);
this.name = name;
}
BillGates.prototype = new Person();
var BillGates = new BillGates("Bill Gates");
BillGates.SayHello(); //alert ------> Hello, I'm Bill Gates of Microsoft
BillGates.show(); //alert ----> hi
BillGates.prototype = new Person();这句话是什么意思 ? 怎么继承的? 不用call也行吗?
JavaScript是通过prototype来创建新对象的,prototype里面的属性是被所有从此prototype中创建出来的对象所公用的。prototype也是一个对象。
[code="js"]BillGates.prototype = new Person();[/code]这句话把BillGates的prototype设成一个Person的对象,这样所有从BillGates生成出来的对象(通过new BillGates()),都共用这个Person对象。
可以参考我写的这篇文章[url]http://www.ibm.com/developerworks/cn/web/wa-lo-dojoajax1/[/url]。