![js apply 和call的区别 js apply 和call的区别](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
function Person(name, profession) {
this.name = name;
this.profession = profession;
this.speak = function () {
document.writeln("我叫"+name+",我是一位"+profession);
}
}
function Student(name,profession) {
Person.call(this,name,profession);
};
function Teacher(name, profession)
{
Person.apply(this, arguments);
}
var student = new Student("张三", "学生");
var teacher = new Teacher("李四", "老师");
student.speak();
teacher.speak();
结果如下
从结果中我们可以看到,apply和call可以实现继承,但call的用法要比apply灵活