内部函数是外部函数的属性吗?
Function.prototype.test = function(){return "F"}
function hh(){var x="xx";function test(){return "f"}}
print(hh.test());
结果是“f”,这是否意味着内部函数是外部函数的属性?
==更新了我的代码,但结果仍然是“f”。 !_!
Function.prototype.test = function(){return "F"}
function hh(){var x="xx";function test(){return "f"}}
print(hh.test());
the result is "f", does that mean the inner function is a property of outer function?
== updated my code, but the result is still "f". !_!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
结果应该是一个错误 - 至少在 Chrome 中是这样。
hh
内的test
是本地函数,不应该从外部访问它。至于
Function.test
,它是Function
的属性 - 不是所有函数的成员。如果您确实想让某些东西成为所有函数的成员,则需要将其添加到Function.prototype
The result should be an error - and at least in Chrome, it is.
test
insidehh
is a local function, and it should not be accessible from outside.As for
Function.test
, it is a property ofFunction
- not a member of all functions. If you actually want to make something a member of all functions, it needs to be added toFunction.prototype
你真的设法从这段代码中得到结果吗?您正在尝试打印结果。
hh.test
将未定义,因为test
被私有定义为 hh。在此实例中,在原型Function.prototype.test
上定义测试将从hh.test
返回“F”。You actually managed to get a result from this code? You are trying to print the result.
hh.test
will be undefined astest
is being defined privately to hh. Defining test on the prototypeFunction.prototype.test
would return 'F' fromhh.test
in this instance.