function funcA() {
this.show = function(str) {
console.log(str);
}
}
function funcB() {
this.read = function() {}
}
var a = new funcA();
var b = new funcB();
funcA.call(b);//use call
a.show("a");
b.show("b");
call和apply效果是一样的,不过是传参方式上不一样,但是推荐用call,因为apply的效率会低很多。
4、复制继承
function funcA(){
this.name="hello";
this.show=function(){
console.log(this.name);
}
}
function funcB(){
this.extend=function(o){
for(var p in o){
this[p]=o[p];
}
}
}
var a=new funcA();
var b=new funcB();
b.extend(a);
b.show();
这个类似于jquery的extend的方法,原理是把a中的属性遍历到b中。