function People(name){ this.name=name; //对象方法 this.Introduce=function(){ alert("My name is "+this.name); }}//类方法People.Run=function(){ alert("I can run");}//原型方法People.prototype.IntroduceChinese=function(){ alert("我的名字是"+this.name);}//测试var p1=new People("Windking");p1.Introduce();People.Run();p1.IntroduceChinese(); 3 obj1.func.call(obj)方法A.prototype = new B();理解prototype不应把它和继承混淆。A的prototype为B的一个实例,可以理解A将B中的方法和属性全部克隆了一遍。A能使用B的方法和属性。这里强调的是克隆而不是继承。可以出现这种情况:A的prototype是B的实例,同时B的prototype也是A的实例。
function baseClass(){ this.showMsg = function() {alert("baseClass::showMsg");}}function extendClass(){}extendClass.prototype = new baseClass();var instance = new extendClass();instance.showMsg(); // 显示baseClass::showMsg我们首先定义了baseClass类,然后我们要定义extentClass,但是我们打算以baseClass的一个实例为原型,来克隆的extendClass也同时包含showMsg这个对象方法。function baseClass(){ this.showMsg = function() {alert("baseClass::showMsg");}}function extendClass(){ this.showMsg =function () {alert("extendClass::showMsg"); }}extendClass.prototype = new baseClass();var instance = new extendClass();instance.showMsg();//显示extendClass::showMsg实验证明:函数运行时会先去本体的函数中去找,如果找到则运行,找不到则去prototype中寻找函数。或者可以理解为prototype不会克隆同名函数。extendClass.prototype = new baseClass();var instance = new extendClass();var baseinstance = new baseClass();baseinstance.showMsg.call(instance);//显示baseClass::showMsg这里的baseinstance.showMsg.call(instance);阅读为“将instance当做baseinstance来调用,调用它的对象方法showMsg”
<script type="text/javascript">function baseClass(){ this.showMsg = function() {alert("baseClass::showMsg");} this.baseShowMsg = function() {alert("baseClass::baseShowMsg"); }}baseClass.showMsg = function(){ alert("baseClass::showMsg static");}function extendClass(){ this.showMsg =function () {alert("extendClass::showMsg"); }}extendClass.showMsg = function(){ alert("extendClass::showMsg static")}extendClass.prototype = new baseClass();var instance = new extendClass();instance.showMsg(); //显示extendClass::showMsginstance.baseShowMsg(); //显示baseClass::baseShowMsginstance.showMsg(); //显示extendClass::showMsgbaseClass.showMsg.call(instance);//显示baseClass::showMsg staticvar baseinstance = new baseClass();baseinstance.showMsg.call(instance);//显示baseClass::showMsg</script>ps:js的Prototype属性 解释及常用方法function Test(){}alert(Test.prototype); // 输出 "Object"给prototype添加属性function Fish(name, color){this.name=name;this.color=color;}Fish.prototype.livesIn="water";Fish.prototype.price=20;接下来让我们作几条鱼:var fish1=new Fish("mackarel", "gray");var fish2=new Fish("goldfish", "orange");var fish3=new Fish("salmon", "white");再来看看鱼都有哪些属性:
for (int i=1; i<=3; i++){var fish=eval_r("fish"+i); // 我只是取得指向这条鱼的指针alert(fish.name+","+fish.color+","+fish.livesIn+","+fish.price);}输出应该是:"mackarel, gray, water, 20""goldfish, orange, water, 20""salmon, white water, 20"你看到所有的鱼都有属性livesIn和price,我们甚至都没有为每一条不同的鱼特别声明这些属性。这时因为当一个对象被创建时,这个构造函数 将会把它的属性prototype赋给新对象的内部属性__proto__。这个__proto__被这个对象用来查找它的属性。
function Employee(name, salary){this.name=name;this.salary=salary;}Employee.prototype.getSalary=function getSalaryFunction(){return this.salary;}Employee.prototype.addSalary=function addSalaryFunction(addition){this.salary=this.salary+addition;} 我们可以象通常那样创建对象:var boss1=new Employee("Joan", 200000);var boss2=new Employee("Kim", 100000);var boss3=new Employee("Sam", 150000);并验证它:
alert(boss1.getSalary()); // 输出 200000alert(boss2.getSalary()); // 输出 100000alert(boss3.getSalary()); // 输出 150000这里有一个图示来说明prototype是如何工作的。这个对象的每一个实例(boss1, boss2, boss3)都有一个内部属性叫做__proto__,这个属性指向了它的构造器(Employee)的属性prototype。当你执行 getSalary或者addSalary的时候,这个对象会在它的__proto__找到并执行这个代码。注意这点:这里并没有代码的复制(和 Example DT8的图表作一下对比)。
