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 })()

 

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

posted @ 2013-06-24 17:09  FreeGod  阅读(198)  评论(0编辑  收藏  举报