zoukankan      html  css  js  c++  java
  • js 继承

    静态方法的继承

     function ClassA(name) {
                this.name = name;
                this.showSub = function (a, b) {
                    alert(a - b);
                }
                this.getName = function () {
                    alert(this.name);
                }
            }
            function ClassB() {
                this.showAdd = function (a, b) {
                    alert(a + b);
                }
            }
            function ClassC(name) {
                ClassA.call(this,name);
                ClassB.call(this);
            }
            var cc = new ClassC("aaaaaaaaaa");
            cc.showAdd(1, 8);
            cc.getName();
            cc.showSub(8, 1);

    原型方法继承

         function Person(name) {
                this.name = name;
            }
            Person.prototype.getName = function () {
                return this.name;
            }
            function Author(name, books) {
                Person.call(this, name);
                this.books = books;
            }
            Author.prototype = new Person();//将其Author的prototype属性指向Person对象实例
            Author.constructor = Author;//定义一个构造函数时,其默认的prototype对象是一个Object类型的实例,其constructor属性会
    //会被自动设置为该共造函数本身。如果手工将其prototype设置为另一个对象,那么新对象不会具有原有对象的constructor值,所以需要重新设定。 Author.prototype.getBooks
    =function(){ return this.books; } var author = []; author[0]=new Author("aa",'JavaScript Design Patterns'); author[1]= new Author("bb",'JavaScript Design Patterns'); alert(author[0].getName()); alert(author[1].getBooks());
  • 相关阅读:
    在Ubuntu-20.04上安装Emacs-27.1
    VSCode 配置 C++ 与 python 编译环境
    cf 1557(div2)
    2021牛客暑期多校训练营8
    2021牛客暑期多校训练营7
    2021暑期cf加训3
    2021牛客暑期多校训练营6
    2021牛客暑期多校训练营5
    3ds Max基本操作
    renren-generator快速生成你搬砖需要的CRUD代码的框架
  • 原文地址:https://www.cnblogs.com/cniteeq/p/3305447.html
Copyright © 2011-2022 走看看