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(); 

注:  原型方法  还可以有其他写法

People.prototype={

IntroduceChinese:function(){
  alert("我的名字是"+this.name);
          }

}

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

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


<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>

作者:轩脉刃(yjf512)
出处:(http://www.cnblogs.com/yjf512/
版权声明:本文的版权归作者与博客园共有。欢迎转载阅读,转载时须注明本文的详细链接。

[参考文章]

http://tech.ddvip.com/2009-05/1243588303121461.html

http://jetway.iteye.com/blog/56533

http://xiaofeizm55333.iteye.com/blog/80913

专注Web开发50年。请加群:

轩脉刃的技术日记,请关注同名公众号:{轩脉刃的刀光剑影},记录日常技术讨论和思考

本文基于署名-非商业性使用 3.0许可协议发布,欢迎转载,演绎,但是必须保留本文的署名叶剑峰(包含链接http://www.cnblogs.com/yjf512/),且不得用于商业目的。如您有任何疑问或者授权方面的协商,请与我联系

 
好文要顶 已关注 收藏该文  
103
1
 
 
 

ADD YOUR COMMENT

 
    1. #51楼 DC2014 2015-04-02 11:55
      很好 理解了 有时间要验证一下
    2. #52楼 知鸟 2015-04-13 11:18
      JS中的phototype是JS中比较难理解的一个部分
      有个字母错误 phototype —》 prototype

      望 博主的js代码别用C#代码格式显示

    3. #53楼 丶X線長 2015-05-18 16:31
      好绕啊 晕了!
    4. #54楼 安仲炜 2015-05-26 19:17
      以前没接触过这东西,很好,慢慢的尝试在项目中使用一下,不介意的话我转了,可恨博客园没有收藏功能,要不就不用这么麻烦的转载了,
    5. #55楼 boboChina520 2015-05-29 15:53
      之前看了一些相关书籍,看了博主的文章,顿时有种水到渠成的感觉。嘎嘎……
    6. #56楼 Moersing 2015-05-31 17:10
      补充下博主关于原型链继承的几个问题: 
      1.原型链中的引用类型。 
      2.原型链不要互相依赖,GC可没那么好心帮你创建一个临时存储区域。

      1 function base () {
      this.arr = [];

      function sub (){


      sub.prototype= new base(); 
      var a = new sub(); 
      var b = new sub(); 
      console.log(b.arr.length); //0
      a.arr[0]='moersing'; 
      console.log(b.arr.length); //1

      可以看出,arr是一个共享的,如果说不适用this.arr而用 var arr = [] 的话,sub访问不到,只能通过父类的闭包来访问,但是问题还是存在的。如果想继承一个完全独立的引用类型: 
      第一 : 
      function base () {
      this.arr = [];

      function sub (){ 
      base.call(this);// 先加上一个实例属性

      sub.prototype= new base(); 
      var a = new sub(); 
      var b = new sub(); 
      console.log(b.arr.length); //0
      a.arr[0]='moersing'; 
      console.log(b.arr.length); //0 
      console.log(a.arr.length); //修改了a,b不受影响 
      这个问题可以解决,但是不完美,可以看出, a和b有一个实例属性arr和一个prototype的arr,也就是说。 
      a.arr[0] = 'moersing' ; 
      console.log(a.arr.length); //1 
      delete a.arr; //把属性实例干掉 
      console.log(a.arr.length);// 这个时候访问的是 prototype的。 
      所以,理论上来讲,这并不能算是完美的,变量也是需要内存的不是吗?好OK,那么,接下来,使用另一种,上面那个叫 借用继承。 
      接下来这种方式比较完美,但是也不是那么复杂,主要是使用借用继承的思想罢了,也就是说,借用实例和借用原型分开。 
      function base() {
      this.arr = [];
      }
      base.Constructor = function () { //添加一个原型构造函数
      //这里判断一下,如果是一个函数,并且不是一个正则表达式对象,IE7会把正则反映成一个function而不是一个object
      if (typeof this === 'function' && typeof this.prototype.source === 'undefined') {
      var fn = this.prototype;
      fn.name = 'moersing';
      fn.age = '18';
      fn.getFull = function () {
      console.log(this.name + '|' + this.age);
      };
      }
      else {
      throw new TypeError('this is not a function');
      }
      };
      function sub() {
      base.call(this); //借用构造函数,实例化一个实例属性
      }
      base.Constructor.call(sub); //再调用父类的方法,构造出一个prototype的。
      var a = new sub();
      var b = new sub();
      a.arr[0] = 'moersing'; //给a实例的引用类型添加一个元素
      console.log(a.arr.length); //结果是1
      console.log(b.arr.length); //结果是0,也就是没有收到影响
      console.log(a.name); //打印a.prototype的属性
      console.log(b.name); //打印b.prototype的属性
      b.name = 'linfo'; //修改b的。
      console.log(b.name); //linfo
      console.log(a.name); //a没有影响,还是moersing
      a.getFull(); //moerisng|18
      b.getFull(); //linfo |18

js的prototype(2)的更多相关文章

  1. 深入理解js的prototype以及prototype的一些应用

    上一篇讲了js的prototype概念,在这里回顾一下prototype的定义: prototype是函数的一个属性,并且是函数的原型对象.引用它的必然是函数,这个应该记住. 但是,很奇怪,各位看官, ...

  2. 简单理解js的prototype属性

    在进入正文之前,我得先说说我认识js的prototype这个东西的曲折过程. 百度js的prototype的文章,先看看,W3School关于prototype的介绍: 你觉得这概念适合定义js的pr ...

  3. Bom和Dom编程以及js中prototype的详解

    一.Bom编程: 1.事件练习: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  4. JS的prototype和__proto__(含es6的class)

    JS的prototype和__proto__(含es6的class) 一.prototype和__proto__的概念 prototype是函数的一个属性(每个函数都有一个prototype属性),这 ...

  5. JS的prototype和__proto__、constructor

    看了JS的prototype和__proto__这篇文章,才感觉很清晰了,对于原型这块,以前经常把这些属性弄不清楚, 明白了之后保存下整理下: prototype: 是函数的一个属性(每个函数都有一个 ...

  6. js的prototype的详解(1)

    一.什么是JavaScript中对象的prototype属性 JavaScript中对象的prototype属性,是用来返回对象类型原型的引用的.我们使用prototype属性提供对象的类的一组基本功 ...

  7. JS 测试 Prototype

    JS 测试 Prototype 测试 JavaScript 框架库 - Prototype 引用 Prototype 如需测试 JavaScript 库,您需要在网页中引用它. 为了引用某个库,请使用 ...

  8. 复习一下js的prototype 属性

    <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</t ...

  9. 如何使用 js 扩展 prototype 方法

    如何使用 js 扩展 prototype 方法 expand prototype function enhancedLog(msg = ``) { // this.msg = msg; enhance ...

  10. js & object & prototype & __proto__ & prototype chain

    js & object & prototype & proto & prototype chain constructor prototype === instance ...

随机推荐

  1. 《DSP using MATLAB》Problem 2.6

    1.代码 %% ------------------------------------------------------------------------ %% Output Info abou ...

  2. [CF1016G]Appropriate Team

    codeforces description 给你一个数组\(\{a_i\}\)以及\(X,Y\),问你有多少对\((i,j)\)满足存在一个\(v\in \mathbb{N}^+\)使得 \(\gc ...

  3. 唯品会的Service Mesh三年进化史

    每种架构风格,都会因各公司面临的情况不同而有不同的实现路线,Service Mesh也不例外,比如江南白衣描述的唯品会的服务化体系开放服务平台OSP(Open Service Platform)走的S ...

  4. Linux之 linux7防火墙基本使用及详解

    1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld  停止: systemctl disab ...

  5. 【翻译】HTML5开发——轻量级Web Database存储库html5sql.js

    方式1: html5sql官方网址:http://html5sql.com/ 阅读之前,先看W3C关于WEB Database的一段话: Beware. This specification is n ...

  6. [转]Spring 之 Log4j 的配置

  7. Hive-表连接

    Hive只支持等值连接,即ON子句中使用等号连接,不支持非等值连接. Hive内置的数据存储类型,TextFile, SequenceFile, ORC(列式存储) 如果连接语句中有WHERE子句,会 ...

  8. c#中的dynamic类型

    dynamic是C#4.0引入的全新类型,它允许其操作略过编译期类型检查,而在运行时期处理. dynamic类型在大多数情况下和object类似,不同点在于编译器对于dynamic类型的 数据不做进一 ...

  9. QQ中的国家城市数据。

    在QQ安装目录可以找到loclist.xml文件 C:\Program Files\Tencent\QQ\I18N\2052 以上是中文版,若需要英文版,可安装国际版...在1033目录 code都是 ...

  10. Backing up the tail

    The tail of the transaction log usually refers to the contents of the database's transaction log tha ...