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());
  • 相关阅读:
    TP5.x——打印SQL语句
    PHP——运行shell命令|脚本
    Git——取消merge状态
    Typecho——简介及安装
    Vue——服务器上部署vue.js
    Node——服务器上安装Node.js
    PHP——敏感词过滤
    PHP——emjoin表情存入数据库
    什么是脚本语言
    全局拦截各种http请求
  • 原文地址:https://www.cnblogs.com/cniteeq/p/3305447.html
Copyright © 2011-2022 走看看