zoukankan      html  css  js  c++  java
  • 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();

  • 相关阅读:
    PHP 使用 header()实现重定向
    PHP抑制符号 @
    PHP自动加载spl_autoload_register
    打开PHP错误提示
    通过htaccess使用伪静态
    用反引号(``)标注表明或者字段名,防止跟 mysql关键字冲突
    Bootstrap 弹出框(Popover)插件
    jQuery 的 validator 验证,以及添加自定义验证规则。
    线程与并发(一) 多线程基础
    SpringCloud入门
  • 原文地址:https://www.cnblogs.com/qiuh/p/3039819.html
Copyright © 2011-2022 走看看