zoukankan      html  css  js  c++  java
  • javascript类式继承1

    ---恢复内容开始---

    要注意的一点是:extend只负责继承你原型中的方法,如果要继承父类的属性必须在子类显示call调用
    1
    (function () { 2    3 function extend(subClass, superClass) { 4 function f() { 5 } 6      //防止超类的构造函数太过繁杂 7 f.prototype = superClass.prototype; 8 subClass.prototype = new f(); 9 subClass.prototype.constructor = subClass;//虽然constructor没多大用,但是最好自己还是让他保持本身的样子。 10 } 11 12 function Person(age) { 13 this.age = age; 14 } 15 16 Person.prototype.getAge = function () { 17 return this.age; 18 } 19 20 function Author(age, books) { 21 Person.call(this, age);//继承父类属性 22 this.books = books;//加入自己的属性 23 } 24 25 extend(Author, Person); 26 var a1 = new Author(21, "asd"); 27 console.log(a1.getAge()); 28 29 30 })()

    ---恢复内容结束---

  • 相关阅读:
    伍佰《突然的自我》
    .NET常见ORM框架
    并发和压测工具
    底层源码调试工具
    c 冒泡排序
    c 指定范围的质数
    c 筛法列举质数
    c 牛顿法求方程近似解
    c 二分法求方程近似解
    css选择器 及其权重
  • 原文地址:https://www.cnblogs.com/qiutiantian/p/3152851.html
Copyright © 2011-2022 走看看