[英]How to call child function which is declared inside another function
Function b is declared inside function a. 函数b在函数a中声明。 So, i am trying to call function b outside function a.
所以,我试图在功能a之外调用功能b。 It is not getting called.
它没有被调用。
var a = function() {
alert("a");
var b = function() {
alert("b");
}
}
Output:- function b called Alert box with message b 输出:-功能b称为带有消息b的警报框
You'd need to return b
and use that. 您需要返回
b
并使用它。 This will involve calling the function a
to begin with, but then you'd have a function which you could call repeatedly to alert b
: 这将涉及调用函数
a
开头,但随后您将拥有一个可以反复调用以提醒b
的函数:
var a = function() { alert("a"); var b = function() { alert("b"); } return b; } var messageB = a(); messageB(); messageB();
Try to declare 'b' variable outside 'a' function: 尝试在“ a”函数外部声明“ b”变量:
var b;
var a = function() {
alert("a");
b = function() {
alert("b");
};
};
That way, you can access b
from outside of a
as it is declared outside of it. 这样,您可以访问
b
从外面a
,因为它是在它之外声明。 Before a
was called the first time, it will be undefined
though, so you can't call b
before a
. 前
a
被称为第一次,这将是undefined
的,所以你不能调用b
前a
。 Additionally if you call a
twice, the previous b
will be overriden., as shown here: 此外,如果你调用
a
两次,以前b
将被重写,如下所示:
var b;
var a = function(name) {
b = function() { alert(name); };
};
a("first version");
b(); // first version
a("second version");
b(); // second version
If you don't need any inner variables from a
inside b
, there is no reason to nest the functions though, in that case you should just have two independent functions: 如果你不需要任何内部变量
a
内部b
,没有理由嵌套虽然功能,在这种情况下,你应该有两个独立的功能:
var a = function() { alert("a"); };
var b = function() { alert("b"); };
First return that function and call it, 首先返回该函数并调用它,
var a = function() { alert("a"); var b = function() { alert("b"); } return b; } var b = a(); b();
As mentioned you can simply return function b. 如前所述,您可以简单地返回函数b。 Functions in Javascript can be passed around like this, it's a very nice feature of a garbage collected language.
Java语言中的函数可以像这样传递,这是垃圾收集语言的一个很好的功能。
But you can go even better, what if you wanted to expose more than one function. 但是您可以做得更好,如果您想公开多个功能呢?
You can just return an object with methods attached. 您可以只返回带有附加方法的对象。
function A() { let value = 0; return { inc: () => ++ value, dec: () => -- value, show: () => console.log(value) } } const a1 = A(); a1.inc(); a1.inc(); a1.show(); const b1 = A(); b1.inc(); b1.dec(); b1.dec(); b1.show();
Now the above looks very similar to what's called a class
but it's not. 现在,上面的内容看起来与所谓的
class
非常相似,但事实并非如此。 One disadvantage to this is if you wanted to create thousands of instances, a class would be better as that can put it's methods on the prototype. 这样做的一个缺点是,如果您想创建数千个实例,则一个类会更好,因为它可以将其方法放在原型上。 But saying that, as has been found using React Hooks it's not really that bad then.
话虽如此,正如使用React Hooks所发现的那样,那时还不是很糟糕。
One big advantage of the above, is you don't have to get into the muddy world of this
context of instances. 上述的一个大好处,就是你不必陷入泥泞的世界
this
实例的背景下。
For completeness here is a class version. 为了完整起见,这里是一个类版本。
class A { value = 0; inc() { ++ this.value; } dec() { -- this.value; } show() { console.log(this.value) } }; const a1 = new A(); a1.inc(); a1.inc(); a1.show(); const b1 = new A(); b1.inc(); b1.dec(); b1.dec(); b1.show();
It was also said a simple solution is to make a
and b
both global, but the more you avoid the global the better, so if a
and b
have some common intent, using closures or even a class would be better. 也有人说一个简单的解决方案是使
a
和b
都成为全局变量,但越避免使用全局变量越好,因此,如果a
和b
有共同的意图,则使用闭包甚至一个类会更好。
or if you would need object approch 或者如果您需要对象批准
`var a = function() {
print("a");
this.b = function() {
print("b");
}
return this
}
a().b()`
All the other answer's are great enough, but just in case you may like this approach of directly calling
inner function
without returning
that function. 所有其他答案都足够好,但是以防万一您可能喜欢
directly calling
inner function
without returning
该函数的方法。
You can simply wrap your inner function ie b
in your case, as a self invoking function like this, 您可以将内部函数(即
b
包装成这种情况,作为这样的自调用函数,
var a = function() {
alert("a");
var b = (function() {
alert("b");
})()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.