码迷,mamicode.com
首页 > Web开发 > 详细

JS继承封装

时间:2015-02-25 11:31:34      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

<script>
var extend = function (subClass, superClass) {
//1.继承类的中间类
var Tmp = function() {};
//2.将父类的原型传递给中间类
Tmp.prototype = superClass.prototype;
//3.设值继承关系
subClass.prototype = new Tmp();
subClass.prototype.constructor = subClass;
//4.将父类的this转换成当前类的this
subClass.convertASThis = superClass.prototype;
}

function Person(name) {
this.name = name;
}
function Author(name, books) {
//使用call方法将父类的this转换为本类的this,并将name属性合并到当前类
Author.convertASThis.constructor.call(this, name);
this.books = books;
this.getInfo = function() {
return this.name + " -> " + this.books;
}
}

onload = function() {

extend(Author, Person);

var a = new Author("aa", "bb");

alert(a.getInfo());
}
</script>

JS继承封装

标签:

原文地址:http://www.cnblogs.com/zzq-include/p/4299326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!