所有的Function类的对象都具有这个叫call()的方法,它接受一个this操作符的宿主环境对象,还接受一个参数列表,注意不是参数数组。
这篇文章使用call()方法的第一个参数的特点来实现继承。
// 基类 function Person(name, sex) { this.name = name; this.sex = sex; this.get_nama = function(){ return this.name; }; this.get_sex = function(){ return this.sex; }; } var man1 = new Person('路人甲', '男'); document.write(man1.get_nama() + ',性别:' + man1.get_sex() + '<br>'); // 子类 function PHPer(name, sex, title) { // 改变Person对象中this的宿主环境 Person.call(this, name, sex); this.title = title; this.get_title = function(){ return this.title; }; } var man2 = new PHPer('胡子哥', '女', 'php研发工程师'); document.write(man2.get_nama() + ',性别:' + man2.get_sex() + ',职称:' + man2.get_title());