我觉得最清晰的方式是试用prototype,如下:

        var animal = function(){  
            this.name = 'pipi';    
            this.age = 10;      
            this.height = 0;
        }

        var cat = function() {
            this.play = function() {
                alert("cat play");
            }
            cat.prototype = new animal();
        }
        var cat1 = new cat();
        alert(cat1.name);
 

使用prototype时要注意使用父类构造函数必须没有任何参数。如果构造函数中有参数便不能完全的继承,只能继承父类通过prototype初始的属性和方法,在构造函数中初始的属性和方法便不会继承。

相关文章:

  • 2021-11-12
  • 2021-12-09
  • 2021-10-27
  • 2021-07-08
  • 2021-11-08
  • 2021-05-25
  • 2021-12-12
猜你喜欢
  • 2020-12-27
  • 2021-07-30
  • 2022-01-22
  • 2022-12-23
  • 2021-09-26
  • 2021-05-24
  • 2022-01-21
相关资源
相似解决方案