简体   繁体   English

内部函数是外部函数的属性吗?

[英]is the inner function a property of outer function?

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? 结果为“ f”,是否表示内部函数是外部函数的属性?

== updated my code, but the result is still "f". ==更新了我的代码,但结果仍然是“ f”。 !_! !_!

The result should be an error - and at least in Chrome, it is. 结果应该是错误-至少在Chrome中是这样。

test inside hh is a local function, and it should not be accessible from outside. hh内部的test是局部函数,不应从外部访问。

As for Function.test , it is a property of Function - not a member of all functions. 至于Function.test ,它的属性Function -不是所有功能的成员。 If you actually want to make something a member of all functions, it needs to be added to Function.prototype 如果您实际上想使某个东西成为所有函数的成员,则需要将其添加到Function.prototype

You actually managed to get a result from this code? 您实际上设法从此代码中得到了结果? You are trying to print the result. 您正在尝试打印结果。

hh.test will be undefined as test is being defined privately to hh. hh.test将是未定义的,因为test是私自定义为hh。 Defining test on the prototype Function.prototype.test would return 'F' from hh.test in this instance. 在这种情况下,在原型Function.prototype.test上定义test将从hh.test返回'F'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM