zoukankan      html  css  js  c++  java
  • javascript中的继承方式

    javascript中的继承方式有好几种。

    下面分别举例供大家参考学习:

    1.function parent()

    {

      this.x=1;
    }

    function child()

    {

       var instance=new parent();//实例化父类

       for(var i in instance)

       {

          this[i]=instance[i];//将父类中的元素匹配给她的子类
       }  
    }

    var c = new child();

    alert(c.x);

    2.父类同上

    function child()

    {

       this.parent=parent;

       this.parent();

       delete this.parent;

    }

    var c = new child();

    alert(c.x);

    3.父类同上

    这次用js提供的Call方法

    functon child()

    {

       parent.call(this);
    }

    var c = new child();

    alert(c.x);

    原型如下:

    function parent(){
    }
    parent.prototype.x
    =1;

    function child(){
    }
    for(var p in parent.prototype)child.prototype[p]=parent.prototype[p];
    var c=newchild();
    alert(c.x);

    function parent(string){
       
    var child=new Function("this.x=1;"+string);
       
    return child;
    }
    var child=new parent("this.y=2;");
    var c=new child();
    alert(c.y);

    function parent(){
       
    this.x=1;
    }
    function child(){
    }
    child.prototype
    =new parent();
    var c=new child();
    alert(c.x);

     function parent(){
       
    this.x=1;
    }
    function child(){
       
    var ret=new parent();
        ret.y
    =2;
       
    return ret;
    }
    var c=new child();
    alert(c.x);

    本文采编于租赁宝网内部技术人员 参考网址:http://www.zulinbao.com

  • 相关阅读:
    Python开发WebService--使用soaplib库
    weblogic
    cronttab命令
    redhat下配置VNC远程客户端连接
    Linux主机名域名修改问题
    使用expdp命令自动备份数据库
    Linux下内存管理
    Linux下用户和用户组管理
    虚拟机上安装vmware tool
    linux基本信息查询
  • 原文地址:https://www.cnblogs.com/systemxgl/p/1864498.html
Copyright © 2011-2022 走看看