js中的prototype,js中prototype,js的prototypeprototype是javascript中笔记难理解的一部分内容,下面通过几个关键知识点给大家讲解js中的prototype。具体内容请看下文详情。 1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个类B,这个类是以A为原型的,并且能进行扩展。我们称B的原型为A。 2 javascript的方法可以分为三类: a 类方法 b 对象方法 c 原型方法 例子: 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)方法 意思是将obj看成obj1,调用func方法 好了,下面一个一个问题解决: prototype是什么含义? javascript中的每个对象都有prototype属性,Javascript中对象的prototype属性的解释是:返回对象类型原型的引用。 理解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这个对象方法。 extendClass.prototype = new baseClass()就可以阅读为:extendClass是以baseClass的一个实例为原型克隆创建的。 那么就会有一个问题,如果extendClass中本身包含有一个与baseClass的方法同名的方法会怎么样? 下面是扩展实验2: 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的一个实例instance调用baseClass的对象方法showMsg怎么办? 答案是可以使用call: 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” 好了,这里可能有人会问,为什么不用baseClass.showMsg.call(instance); 这就是对象方法和类方法的区别,我们想调用的是baseClass的对象方法 最后,下面这个代码如果理解清晰,那么这篇文章说的就已经理解了: <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属性 解释及常用方法 函数:原型 每一个构造函数都有一个属性叫做原型(prototype,下面都不再翻译,使用其原文)。这个属性非常有用:为一个特定类声明通用的变量或者函数。 prototype的定义 你不需要显式地声明一个prototype属性,因为在每一个构造函数中都有它的存在。你可以看看下面的例子: Example PT1 CODE:
function Test(){}alert(Test.prototype); // 输出 "Object" 给prototype添加属性 就如你在上面所看到的,prototype是一个对象,因此,你能够给它添加属性。你添加给prototype的属性将会成为使用这个构造函数创建的对象的通用属性。 例如,我下面有一个数据类型Fish,我想让所有的鱼都有这些属性:livesIn="water"和price=20;为了实现这个,我可以给构造函数Fish的prototype添加那些属性。 Example PT2 CODE:
function Fish(name, color){this.name=name;this.color=color;}Fish.prototype.livesIn="water";Fish.prototype.price=20; 接下来让我们作几条鱼: CODE: var fish1=new Fish("mackarel", "gray");var fish2=new Fish("goldfish", "orange");var fish3=new Fish("salmon", "white"); 再来看看鱼都有哪些属性: CODE:
for (int i=1; i<=3; i++){var fish=eval_r("fish"+i); // 我只是取得指向这条鱼的指针alert(fish.name+","+fish.color+","+fish.livesIn+","+fish.price);} 输出应该是: CODE: "mackarel, gray, water, 20""goldfish, orange, water, 20""salmon, white water, 20" 你看到所有的鱼都有属性livesIn和price,我们甚至都没有为每一条不同的鱼特别声明这些属性。这时因为当一个对象被创建时,这个构造函数 将会把它的属性prototype赋给新对象的内部属性__proto__。这个__proto__被这个对象用来查找它的属性。 你也可以通过prototype来给所有对象添加共用的函数。这有一个好处:你不需要每次在构造一个对象的时候创建并初始化这个函数。为了解释这一点,让我们重新来看Example DT9并使用prototype来重写它: 用prototype给对象添加函数 Example PT3 CODE: 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;} 我们可以象通常那样创建对象: CODE:
var boss1=new Employee("Joan", 200000);var boss2=new Employee("Kim", 100000);var boss3=new Employee("Sam", 150000); 并验证它: CODE:
alert(boss1.getSalary()); // 输出 200000alert(boss2.getSalary()); // 输出 100000alert(boss3.getSalary()); // 输出 150000 这里有一个图示来说明prototype是如何工作的。这个对象的每一个实例(boss1, boss2, boss3)都有一个内部属性叫做__proto__,这个属性指向了它的构造器(Employee)的属性prototype。当你执行 getSalary或者addSalary的时候,这个对象会在它的__proto__找到并执行这个代码。注意这点:这里并没有代码的复制(和 Example DT8的图表作一下对比)。

js中的prototype,js中prototype,js的prototype
|