js中call()和apply()方法很常用,因为他们可以将函数绑定到其他对象上实行,从而实现继承,php点点通示例如下:
<script>
function Person(name,age){
this.name = name;
this.age = age;
this.say = function(){
document.write("我的姓名是:"+this.name+",我的年龄是:"+this.age+"<br />");
}
}
//call()方法继承Person类
function BlackMan(name,age){
Person.call(this,name,age);
}
//apply()方法继承Person类
function WhiteMan(name,age){
Person.apply(this,[name,age]);
}
var mckee = new Person("mckee",20);
mckee.say();
var kobe = new BlackMan("科比",34);
kobe.say();
var white =new WhiteMan("小白",22);
white.say();
</script>