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. minio 集群搭建

      具体实际的取舍可以参考官方文档,我使用的是4 node 4 driver 模式 环境机器说明 192.168.31.2 192.168.31.3 192.168.31.4 192.168.31.5 ...

  2. FastAdmin 的前端环境怎么安装?

    FastAdmin 的前端环境怎么安装? 安装 Git 安装 Node.js 安装 cnpm 安装 bower 开始安装 FastAdmin 的前端组件 bower install bower upd ...

  3. Angular 安装

    1.angular安装 npm  install -g angular 2. 显示angular安装路径 npm config ls 3. angular 双向绑定 <!DOCTYPE html ...

  4. panabit允许一台代理服务器只能收QQ企业邮箱,和内网ip通讯,限制除了QQ企业邮箱以外的所有内容规则

    环境: 可访公网网的内网网段:192.168.0.0/24(员工网段)  192.168.2.0/24(服务器网段)两个内网网段. 不能访问公网的内网网段:192.168.4.0/24 4网段利用fo ...

  5. SpringCloud初体验:四、API GateWay 服务网关

    网关服务很多,比如:Zuul.Kong.spring cloud gateway ……, 这里不纠结哪种性能好,本次体验是用的 spring cloud gateway 更多网关比较可以了解这篇文章: ...

  6. linux Posix 信号量 一

    信号量是一种用于提供不同进程间或一个给定进程的不同线程间同步手段的原语. linux提供两种信号量,“内核信号量”和“用户态进程信号量”,“用户态信号量”又分为“Posix”,“System V”信号 ...

  7. eclipse+maven springMVC搭建

    1.新建项目: 选择Maven Project 选择项目位置,这里我选择的是C:\Users\admin\workspace\practice 选择maven项目类型,这里选择webapp: 填写Gr ...

  8. Hive 安装操作

    本篇为安装篇较简单: 前提:1: 安装了hadoop-1.0.4(1.0.3也可以)正常运行2:安装了hbase-0.94.3, 正常运行 接下来,安装Hive,基于已经安装好的hadoop,步骤如下 ...

  9. C++Builder 也有StringBuilder

    System::Sysutils::TStringBuilder *sb = new TStringBuilder;    sb->Append("ww");    sb-& ...

  10. 开关 toggleClass('hide')

    toggleClass 实现属性的反转 <!DOCTYPE html> <html lang="en"> <head> <meta cha ...