zoukankan      html  css  js  c++  java
  • js的prototype(2)

    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

    Creative Commons License

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

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

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

     
    好文要顶 已关注 收藏该文  
    103
    1
     
     
     
    « 上一篇:Web站点的传输--《构建高性能Web站点》阅读1
    » 下一篇:PHP中间件--ICE

    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
  • 相关阅读:
    如何把pdf文档转化为word
    Win7系统中wmiprvse.exe占用CPU高如何解决
    Visual studio加载项目时报错 尚未配置为Web项目XXXX指定的本地IIS,需要配置虚拟目录。解决办法。
    SQL Server新增用户并控制访问权限设置。
    vs2013 中已经添加了引用,编译还是提示没有添加引用
    vue-cli中引入jquery方法
    vue-vuex安装
    vue2.0项目结构和打包发布
    从(0)新开始vue2.0【安装】
    js判断网络连接情况:navigator.onLine
  • 原文地址:https://www.cnblogs.com/keyi/p/6991972.html
Copyright © 2011-2022 走看看