• js中的继承1--类继承


    继承:
    function Animal(name){
    this.name = name;
    this.showName = function(){
    alert(this.name);
    }
    }

    function Cat(name){
    Animal.call(this, name);
    }

    var cat = new Cat("Black Cat");
    cat.showName();

     Animal.call(this) 的意思就是使用 Animal对象代替this对象,那么 Cat中不就有Animal的所有属性和方法了吗,Cat对象就能够直接调用Animal的方法以及属性了.

    多继承:



    function Class10()
    {
    this.showSub = function(a,b)
    {
    alert(a-b);
    }
    }

    function Class11()
    {
    this.showAdd = function(a,b)
    {
    alert(a+b);
    }
    }

    function Class2()
    {
    Class10.call(this);
    Class11.call(this);
    }

    很简单,使用两个 call 就实现多重继承了
    当然,js的继承还有其他方法,例如使用原型链,这个不属于本文的范畴,只是在此说明call 的用法。说了call ,当然还有 apply,这两个方法基本上是一个意思,区别在于 call 的第二个参数可以是任意类型,而apply的第二个参数必须是数组,也可以是arguments。

    参考:http://www.studyofnet.com/news/419.html

  • 相关阅读:
    js键盘事件以及键盘事件拦截
    JavaScript 延迟加载
    二叉树深度优先 求二叉树最大深度
    css 小知识点:inline/inline-block/line-height
    es6 set
    CSS 水平垂直居中
    js 位运算符
    js 函数重载
    js之单例模式
    js 面向对象 ES5 AND ES6
  • 原文地址:https://www.cnblogs.com/cshi/p/5450009.html
Copyright © 2020-2023  润新知