var clientData = {
id: 094545,
fullName: "Not Set",
// setUserName is a method on the clientData object
setUserName: function (firstName, lastName) {
// this refers to the fullName property in this object
this.fullName = firstName + " " + lastName;
}
}
clientData.setUserName(firstName, lastName)是一个全局函数?
getUserInput ("Barack", "Obama", clientData.setUserName);
console.log (clientData.fullName);// Not Set
这种情况得出 Not Set;
而clientData.setUserName(firstName, lastName);则会得出正确的结论;
这是因为:当把 clientData.setUserName当做是一个参数进行传递时,它仅仅是一个函数,跟上面不同;
把它当做参数时,有点儿像直接返回一个函数,并没有传递其它的东西;
也可以像书上似的,定义一个that给吧name强制添加带上(等于把name锁住),这儿并没有,因此只能是windows了;
为了改变this,只能用apply或call来保证this;
而书上闭包那种形式出现windows则是另一种情况;属于直接调用函数,;this情况特殊;永远属于当前的作用域;
即只要把对象中的这个函数赋值给其它的东西时,这个对象的作用域也就不存在了;
参考:javaScript高级程序设计,
http://blog.csdn.net/luoweifu/article/details/41466537