面試時兩次遇到這個問題,第一次不會,回來查查資料知道了大概,沒想過幾天再次見到,依舊不會,我這腦子,所以,這次自己查資料看高手的總結,在這記錄一下:
1、call()方法定義:
a、定義:調用一個對象的一個方法,以另一個對象替換當前對象。語法爲call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 。
b、說明:call方法可以用來代替另一個對象調用一個方法。可將一個函數的對象上下文從初始的上下文改爲由thisObj指定的新對象。如果沒有提供thisObj參數,那麼Global對象將被用作thisObj。
call()方法示例:
<script>
window.color = 'red';
document.color = 'yellow';
var s1 = {color: 'blue' };
function changeColor(){
console.log(this.color);
}
changeColor.call(); //red (默認傳遞參數)
changeColor.call(window); //red
changeColor.call(document); //yellow
changeColor.call(this); //red
changeColor.call(s1); //blue
</script>
//例2
var Pet = {
words : '...',
speak : function (say) {
console.log(say + ''+ this.words)
}
}
Pet.speak('Speak'); // 結果:Speak...
var Dog = {
words:'Wang'
}
//將this的指向改變成了Dog
Pet.speak.call(Dog, 'Speak'); //結果: SpeakWang
c、轉載的例子:
1》、
function add(a,b)
{
alert(a+b);
}
function sub(a,b)
{
alert(a-b);
}
add.call(sub,3,1);
這個案例的意思就是用add來代替sub,add.call(sub,3,1)===add(3,1),結果爲:alert(4).
2》、
function Animal(){
this.name = "Animal";
this.showName = function(){
alert(this.name);
}
}
function Cat(){
this.name = "Cat";
}
var animal = new Animal();
var cat = new Cat();
//通過call或apply方法,將原本屬於Animal對象的showName()方法交給對象cat來使用了。
//輸入結果爲"Cat"
animal.showName.call(cat,",");
//animal.showName.apply(cat,[]);
call的意思是把animal的方法用到cat上執行,原來cat上是沒有showName()方法的,現在是把animal的showName()方法放到cat上執行,所以,this.name的結果是Cat。
3》、實現繼承
function Animal(name){
this.name = name;
this.showName = function(){
alert(this.name);
}
}
function Cat(name){
Animal.call(this, name);
}
var cat = new Cat("Black Cat");
cat.showName();
Animal.call(this) 的意思就是使用 Animal對象代替this對象,那麼 Cat中不就有Animal的所有屬性和方法了嗎,Cat對象就能夠直接調用Animal的方法以及屬性了.結果當然就是黑貓了。
4》、多重繼承
function Class10()
{
this.showSub = function(a,b)
{
alert(a-b);
}
}
function Class11()
{
this.showAdd = function(a,b)
{
alert(a+b);
}
}
function Class2()
{
Class10.call(this);
Class11.call(this);
}
很簡單,使用兩個call就實現多重繼承了。
2、apply()方法和定義:
a、定義:應用某一個對象的一個方法,用另一個對象替換當前對象。語法爲:apply([thisObj[,argArray]])。
<script>
window.number = 'one';
document.number = 'two';
var s1 = {number: 'three' };
function changeColor(){
console.log(this.number);
}
changeColor.apply(); //one (默認傳參)
changeColor.apply(window); //one
changeColor.apply(document); //two
changeColor.apply(this); //one
changeColor.apply(s1); //three
</script>
//例2
function Pet(words){
this.words = words;
this.speak = function () {
console.log( this.words)
}
}
function Dog(words){
//Pet.call(this, words); //結果: Wang
Pet.apply(this, arguments); //結果: Wang
}
var dog = new Dog('Wang');
dog.speak();
1、function add(c,d){
return this.a + this.b + c + d;
}
var s = {a:1, b:2};
console.log(add.call(s,3,4)); // 1+2+3+4 = 10
console.log(add.apply(s,[5,6])); // 1+2+5+6 = 14
2、<script>
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
</script>