欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
JavaScript call() 方法
 
call() 方法是预定义的 JavaScript 方法。
 
它可以用来调用所有者对象作为参数的方法。
 
通过 call(),您能够使用属于另一个对象的方法。
 
本例调用 person 的 fullName 方法,并用于 person1:
 
实例
 
var person = {
 
    fullName: function() {
 
        return this.firstName + " " + this.lastName;
 
    }
 
}
 
var person1 = {
 
    firstName:"Bill",
 
    lastName: "Gates",
 
}
 
var person2 = {
 
    firstName:"Steve",
 
    lastName: "Jobs",
 
}
 
person.fullName.call(person1);  // 将返回 "Bill Gates"
 
本例调用 person 的 fullName 方法,并用于 person2:
 
实例
 
var person = {
 
    fullName: function() {
 
        return this.firstName + " " + this.lastName;
 
    }
 
}
 
var person1 = {
 
    firstName:"John",
 
    lastName: "Doe",
 
}
 
var person2 = {
 
    firstName:"Mary",
 
    lastName: "Doe",
 
}
 
person.fullName.call(person2);  // 将返回 "Steve Jobs"
 
带参数的 call() 方法
 
call() 方法可接受参数:
 
实例
 
var person = {
 
  fullName: function(city, country) {
 
    return this.firstName + " " + this.lastName + "," + city + "," + country;
 
  }
 
}
 
var person1 = {
 
  firstName:"Bill",
 
  lastName: "Gates"
 
}
 
person.fullName.call(person1, "Seattle", "USA");

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h64463.shtml