quo
不是对象,而是一个简单的函数,并且没有属性(从技术上讲,它可以有,但在这里不适用)。
var quo = function(test) {
function talk() {
return 'yes';
}
/* OR:
var talk = function() {
return 'yes';
}; */
return {
getresult: function() {
// in here, "quo" references the closure function.
// "talk" references the local (function) variable of that "quo" function
return talk();
}
}
}
var myQuo = quo('hi');
myQuo.getresult(); // "yes"
如果你想talk
在对象上获得一个属性“” myQuo
,你需要使用这个:
var quo = function(test) {
return {
talk: function() {
return 'yes';
},
getresult: function() {
// in here, "quo" references the closure.
// "this" references current context object,
// which would be the the object we just return (and assign to "myQuo")
return this.talk();
}
};
}
var myQuo = quo('hi');
myQuo.getresult(); // "yes"
this
在 MDN上阅读有关该关键字的更多信息。