B.apply(A, arguments); //B爲某個函數,A爲某個對象,arguments爲數組 B.call(A, args1,args2);//B爲某個函數,A爲某個對象,後面緊跟着參數
apply:調用一個對象的一個方法,用另外一個對象替換當前對象。javascript
call:調用一個對象的一個方法,用另外一個對象替換當前對象。html
window.firstName = "Cynthia"; window.lastName = "_xie"; var myObject = {firstName:'my', lastName:'Object'}; function getName(){ console.log(this.firstName + this.lastName); } function getMessage(sex,age){ console.log(this.firstName + this.lastName + " 性別: " + sex + " age: " + age ); } getName.call(window); // Cynthia_xie getName.call(myObject); // myObject getName.apply(window); // Cynthia_xie getName.apply(myObject);// myObject getMessage.call(window,"女",21); //Cynthia_xie 性別: 女 age: 21 getMessage.apply(window,["女",21]); // Cynthia_xie 性別: 女 age: 21 getMessage.call(myObject,"未知",22); //myObject 性別: 未知 age: 22 getMessage.apply(myObject,["未知",22]); // myObject 性別: 未知 age: 22
感謝:java