JavaScript——対象継承を実現する5つの方法

3915 ワード

  • まず、thisがjavascriptの中で使用者を指すことを明確にします。javaでは現在のオブジェクトを指します。この両者は本質的に区別されている
  • です。
  • JavaScriptにはクラスの概念がないので、説明対象との関係を引き継ぎ、親クラスのメンバーおよび方法をサブクラスで取得する方式
  • が鍵となります。
    1.相手を偽る
            function Parent(name)
            {
                this.name = name;
                this.sayHello = function()
                {
                    alert(this.name)
                }
            }
    
            function Child(name, age)
            {
                //3                     
                //    this      this,    
                //  Parent  this.name、sayHello,          、     
                this.method = Parent;
                //            
                this.method(name);
                //    
                delete this.method;
                
                //   this    
                this.age = age;
                this.sayWorld = function()
                {
                    alert(age);
                }
            }
            var parent = new Parent("  ");
            var child = new Child("  ", 20);
    
            parent.sayHello();
            child.sayHello();
            child.sayWorld();
    
    2.コールの方法
    //call     Function       ,                 。
    //          call   ,call                   this,
    //   2      ,           
    
    //call     
    function test(str, str2)
    {
        alert(this.name + ", " + str + ", " + str2);
    }
    
    var object = new Object();
    object.name = "zhangsan";
    
    //test.call      test  
    // object   this
    test.call(object, "JavaScript", "hello"); 
    
    
    //call      
     function Parent(name)
        {
            this.name = name;
            this.sayHello = function()
            {
                alert(name);
            }
        }
    
        function Child(name, age)
        {
    //   this    
            Parent.call(this, name);
            this.age = age;
            this.sayWorld = function()
            {
                alert(age);
            }
        }
    
        var parent = new Parent("  ");
        var child = new Child("  ", 20);
    
        parent.sayHello();
        child.sayHello();
        child.sayWorld();
    
    
    3.アプリ方法
    //call   apply                context (   )    ,
    //           this    ,         
    //call   apply         ,             ,apply            
            function Parent(name)
            {
                this.name = name;
                this.sayHello = function()
                {
                    alert(name);
                }
            }
    
            function Child(name, age)
            {
                Parent.apply(this, new Array(name));
    
                this.age = age;
    
                this.sayWorld = function()
                {
                    alert(age);
                }
            }
    
            var parent = new Parent("  ");
            var child = new Child("  ", 30);
    
            parent.sayHello();
            child.sayHello();
            child.sayWorld();
    
    
    4.プロトタイプチェーン方式(prototype chain)
    //          
        function Parent()
        {
    
        }
    
        Parent.prototype.name = "  ";
        Parent.prototype.sayHello = function()
        {
            alert(this.name);
        }
    
        function Child()
        {
    
        }
        //           
        Child.prototype = new Parent();
        Child.prototype.age = 20;
    
        Child.prototype.sayWorld = function()
        {
            alert(this.age);
        }
            var child = new Child();
            child.sayHello();
            child.sayWorld();
    
    
    5.混合方式(プロトタイプの璎の弊害を克服した)
    //              ,      
           function Parent(name)
           {
               this.name = name;
           }
            Parent.prototype.sayHello = function()
            {
                alert(this.name);
            }
    
            function Child(name, age)
            {//   this  parent
                Parent.call(this, name);
                this.age = age;
            }
            //             
            Child.prototype = new Parent();
            Child.prototype.sayWorld = function()
            {
                alert(this.age);
            }
    
            var child = new Child("  ", 30);
            child.sayHello();
            child.sayWorld();