1. 程式人生 > >筆記:js疑難復習

筆記:js疑難復習

.html new his spa 測試 type var 需要 apply

apply 和 call的區別

call 和 apply 的區別只在於這兩個函數接受的參數形式不同

var Person = function(name,age){
    this.name = name;
    this.age = age;
}
Person.prototype.say=function(){
    return this.name;
}

var p1 = new Person(‘測試1號‘,20);
var p2 = new Person(‘測試2號‘,65);
console.log(p1.say());

Person.call(p1, ‘測試1號call‘, 22);//
call 需要把參數按順序傳遞進去(你的參數是明確知道數量時,用 call) console.log(p1.say()); Person.apply(p1, [‘測試1號apply‘, 23]);//apply 則是把參數放在數組裏(你的參數不確定的時候,用 apply) console.log(p1.say()); console.log(p2.say());

http://www.cnblogs.com/humin/p/4556820.html  JS實現繼承的幾種方式

筆記:js疑難復習