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());
原文地址:https://www.cnblogs.com/cniteeq/p/3305447.html