日期:2014-05-16  浏览次数:20325 次

关于prototype问题请教
请问调用prototype 是用函数名调用还是用对象名  
下面这一段代码是可以走通的,employee 这个应该是一个对象,然后用对象调用的prototype
var employee= function (name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}
employee.prototype.salary=null;
bill.salary=20000;

document.write(bill.salary);

但是 把代码改成如下所示
function employee (name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}

var bill=new employee("Bill Gates","Engineer",1985);

bill.prototype.salary=null;
bill.salary=20000;

document.write(bill.salary);
还是用对象调用prototype ,但是不能走通 报错,求解释

------解决方案--------------------
在js中函数也是一种对象,employee.prototype只是employee这个函数的一个属性,和其他属性的区别只在于当使用new employee()创建一个employee的实例对象时,这个实例对象的原型会指向employee.prototype这个对象,也就是employee的实例对象会拥有employee.prototype的所有属性
------解决方案--------------------
bill拥有构造器bill.constructor.prototype.salary 

var bill = new employee("Bill Gates", "Engineer", 1985);

bill.constructor.prototype.salary = 20000;
//bill.salary = 20000;
alert(bill.salary);
实例化的对象拥有构造器constructor构造器指向函数本身,函数拥有原型prototype如果你想修改一个实例化的对象,那你就得通过上面这样