• javascript的继承


    1) 对象冒充方式

    //继承第一种方式:对象冒充

    function Parent(username)
    {
     this.username = username;

     this.sayHello = function()
     {
      alert(this.username);
     }
    }

    function Child(username, password)
    {
     //下面三行代码是最关键的代码
     this.method = Parent;//这样就把this传过去了
     this.method(username);
     delete this.method;

     this.password = password;

     this.sayWorld = function()
     {
      alert(this.password);
     }
    }

    var parent = new Parent("zhangsan");
    var child = new Child("lisi", "1234");

    parent.sayHello();

    child.sayHello();
    child.sayWorld();

    2) call方法方式。
    call方法是Function对象中的方法,因此我们定义的每个函数都拥有该方法。可以通过函数名来调用call方法,call方法的第一个参数会被传递给函数中的this,从第2个参数开始,逐一赋值给函数中的参数。

    //继承的第二种实现方式,call方法方式,Function对象中的方法

    function test(str, str2)
    {
     alert(this.name + ", " + str + ", " + str2);
    }

    var object = new Object();
    object.name = "zhangsan";

    //test.call相当于调用了test函数
    test.call(object, "shengsiyuan", "hello"); //将object赋给了this

    //使用call方式实现对象的继承

    function Parent(username)
    {
     this.username = username;

     this.sayHello = function()
     {
      alert(this.username);
     }
    }

    function Child(username, password)
    {
     Parent.call(this, username);

     this.password = password;

     this.sayWorld = function()
     {
      alert(this.password);
     }
    }

    var parent = new Parent("zhangsan");

    var child = new Child("lisi", "123");

    parent.sayHello();

    child.sayHello();
    child.sayWorld();

    3) apply方法方式

    //使用apply方法实现对象继承

    function Parent(username)
    {
     this.username = username;

     this.sayHello = function()
     {
      alert(this.username);
     }
    }

    function Child(username, password)
    {
     Parent.apply(this, new Array(username));

     this.password = password;

     this.sayWorld = function()
     {
      alert(this.password);
     }
    }


    var parent = new Parent("zhangsan");
    var child = new Child("lisi", "123");

    parent.sayHello();

    child.sayHello();
    child.sayWorld();

    4)原型链方式(无法给构造函数传参数)

    //使用原型链(prototype chain)方式实现对象继承

    function Parent()
    {

    }

    Parent.prototype.hello = "hello";
    Parent.prototype.sayHello = function()
    {
     alert(this.hello);
    }

    function Child()
    {

    }

    Child.prototype = new Parent();

    Child.prototype.world = "world";
    Child.prototype.sayWorld = function()
    {
     alert(this.world);
    }

    var child = new Child();

    child.sayHello();
    child.sayWorld();

    5)混合方式(推荐)

    //使用混合方式实现对象继承(推荐)

    function Parent(hello)
    {
     this.hello = hello;
    }

    Parent.prototype.sayHello = function()
    {
     alert(this.hello);
    }

    function Child(hello, world)
    {
     Parent.call(this, hello);

     this.world = world;
    }

    Child.prototype = new Parent();

    Child.prototype.sayWorld = function()
    {
     alert(this.world);
    }

    var child = new Child("hello", "world");

    child.sayHello();
    child.sayWorld();

  • 相关阅读:
    Redis和Memcache的区别分析
    javascript 与jquery为每个p标签增加onclick方法
    repeater单双行颜色不同,gridview repeater DataList 鼠标经过改变背景颜色
    sql 错误提示
    .net获取select控件中的文本内容
    您试图从目录中执行CGI、ISAPI 或其他可执行程序,但该目录不允许执行程序
    html5 调用摄像头
    openfire配置MSSQL说明(数据库设置)
    Openfire 的安装和配置
    JS 等前端学习。
  • 原文地址:https://www.cnblogs.com/qiuh/p/3039819.html
Copyright © 2020-2023  润新知