var google = {
makeBeer : function(arg1,arg2){
alert([this instanceof google, arg1, arg2]);
}
}
google.makeBeer('water','soda');
上面没有提示当我检查this instanceof google
,但是当我这样做时,相同的代码工作。使用instanceof测试对象
var google = {
makeBeer : function(arg1,arg2){
alert([this instanceof Object, arg1, arg2]);
}
}
google.makeBeer('water','soda');
参考'this'是Object的实例,为什么不是Google Object
。我如何确保实例是我的情况下的特定对象。
更新到同一个问题:
var newWay = {}
google.makeBeer.call(newWay, 'pepsi', 'coke');
在上述情况下,我经过NEWWAY反对我funcion makeBeer,我怎么能知道它的NEWWAY对象这里。
即使在你的更新中,你仍然有一个普通的对象。没有什么特别的。 – Mathletics 2013-03-13 15:26:27
@Mathletics:那很好,但我怎么能区分这两个... – Kevin 2013-03-13 15:27:08
你需要使用这种模式构造你的对象: function Way(){} var newWay = new Way() – 2013-03-13 15:30:35