zoukankan      html  css  js  c++  java
  • inheritPrototypeChain.js

    // 原型链
    // 其基本思路是利用原型让一个引用类型继承另一个引用类型的属性和方法
    
    function Person(){
        this.name = "Person";
    }
    Person.prototype.getName = function(){
        return this.name;
    }
    function SuperPerson(name,sex){
        this.name = name;
        this.sex = sex;
    }
    SuperPerson.prototype = new Person();//重写原型
    
    //在添加新方法或重写原型中的方法时,一定要在重写原型的语句后面,而且不能使用字面量添加新方法,不然又会再一次重写原型,原型链被切断
    
    SuperPerson.prototype.getSex = function(){
        return this.sex;
    }
    
    var Tom = new SuperPerson("Tom","man");
    console.log(Tom.getName())
    console.log(Tom.getSex())
  • 相关阅读:
    Java Spring AOP用法
    Spring IOC的简单实现
    随机数
    Java 正则表达式
    日期格式转换
    maven settings.xml详解
    JSP与Servlet的关系
    EL表达式学习
    FreeMarker学习2
    FreeMarker学习
  • 原文地址:https://www.cnblogs.com/cynthia-wuqian/p/4953751.html
Copyright © 2011-2022 走看看