JS中的prototype

JS中的phototype是JS中比较难理解的一个部分javascript

 

本文基于下面几个知识点:java

 

1 原型法设计模式node

在.Net中可使用clone()来实现原型法设计模式

原型法的主要思想是,如今有1个类A,我想要建立一个类B,这个类是以A为原型的,而且能进行扩展。咱们称B的原型为A。数组

 

2 javascript的方法能够分为三类:函数

a 类方法测试

b 对象方法this

c 原型方法prototype

例子:设计

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属性的解释是:返回对象类型原型的引用。

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这个对象方法。

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的对象方法

------------------------------------------------------------------------------------------------------------------------------------------------------------------

对象方法和类方法的区别:

一、对象方法理解就很简单了,主要是若是类生成一个实例,那么该实例就能使用该方法
二、类方法,不须要经过生成实例就可使用的方法
三、原型方法主要是用来对JS已有的系统对象进行扩展而生的,例如Array数组没有什么方法,你能够为其增长原型方法,那么建立的数组就拥有了该方法。

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

一、对象方法包括构造函数中的方法以及构造函数原型上面的方法;
二、类方法,其实这里的类就是一个函数,在js中因为函数也是一个对象,因此能够为函数添加属性以及方法,这种方法在node中用的比较多;
三、原型方法通常用于对象实例共享,好比Person.prototype.sayName=function(){console.log(this.name);};在原型上面添加该方法,就能实现共享。这样就不用每一次初始化一个实例的时候,为其分配相应的内存了。

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------

最后,下面这个代码若是理解清晰,那么这篇文章说的就已经理解了:

<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::showMsg
instance.baseShowMsg(); //显示baseClass::baseShowMsg
instance.showMsg(); //显示extendClass::showMsg

baseClass.showMsg.call(instance);//显示baseClass::showMsg static

var baseinstance = new baseClass();
baseinstance.showMsg.call(instance);//显示baseClass::showMsg

</script>
相关文章
相关标签/搜索