var aHello = {
name : "hello",
showName : function(){
console.log(this.name);
//console.log(this.tagName);
}
}
document.querySelector(‘a‘).onclick = aHello.showName;
var aHello = {
name : "hello",
showName : function(){
console.log(this.name);
}
}
document.querySelector(‘a‘).onclick = function(){
aHello.showName();
}
var aHello = {
name : "hello",
showName : function(){
console.log(this.name);
}
}
document.querySelector(‘a‘).onclick = aHello.showName.bind(aHello);
var aHello = {
name : "hello",
setYourAge : function(name,age){
console.log(name);
console.log(age);
}
}
document.querySelector(‘a‘).onclick = aHello.setYourAge.call(aHello,‘王佳欣‘,25);
aHello.setYourAge.call(aHello,‘王佳欣‘,25);
aHello.setYourAge.apply(aHello,[‘王佳欣‘,25]);
aHello.setYourAge(‘王佳欣‘,25);
var aHello = {
name : "hello",
setYourAge : function(name,age){
console.log(name);
console.log(age);
}
}
document.querySelector(‘a‘).onclick = aHello.setYourAge.bind(aHello,‘王佳欣‘,25);
aHello.setYourAge.call(aHello,‘王佳欣‘,25);
aHello.setYourAge.apply(aHello,[‘王佳欣‘,25]);
aHello.setYourAge(‘王佳欣‘,25);
/*通用验证对象*/
var validator = {
validateName : function(){
console.log(this.name);
},
validateAge : function(){
console.log(this.age)
}
//.....
}
/*对象kobe*/
var kobe = {
name : ‘kobe bryant‘,
age : -1
}
/*对象 allen*/
var allen = {
name : ‘allen iverson‘,
age : 10
}
var isKobeAgeValid = validator.call(kobe);
var isAllenAgeValid = validator.call(allen);
原文:http://www.cnblogs.com/suhanxuan/p/5838362.html