简体   繁体   English

return语句中的Javascript函数

[英]Javascript function within return statement

I'm starting new with javascript and still learning. 我从javascript开始新的学习,并且还在学习。 I came across this code with different function declaractions. 我遇到了带有不同函数声明的这段代码。 What are the differences between funcA , funcB , and funcX ? funcAfuncBfuncX什么funcX In the code funcA , funcB are used within the module, and funcX & funcY are called from outside the module as someModule.funcX() . 在代码funcAfuncB在模块内使用, funcXfuncY在模块外部作为someModule.funcX()调用。 Likely very simple but I don't know what this is called to even do a rearch. 可能非常简单,但我不知道进行什至是什么rearch。 Any pointers will be appreciated. 任何指针将不胜感激。

var someModule = (function() {
    var funcA = (function() {...})();
    var funcB = function() {...};

    return {
        funcX: function(){...},
        funcY: function(){...}
    };
})();

If you just want the difference between all of the functions here is the difference. 如果您只是想要所有功能之间的差异,这里就是差异。

First difference between funcA , funcB and funcX , funcY . funcAfuncBfuncXfuncY之间的第一个区别。

Whatever we have inside return statement will be accessible by someModule . 无论我们有内部return声明将是访问someModule So we can do both of these below 所以我们可以在下面做这两个

someModule.funcX();
someModule.funcY();

But we cannot do someModule.funcA() or someModule.funcB() . 但是我们不能执行someModule.funcA() or someModule.funcB() To understand why that we can do this is to understand how () this behaves. 要了解为什么可以这样做,就是要了解()行为。 Whatever expression is put inside brackets is executed and result is returned there. 放在括号内的任何表达式都将执行,并在此返回结果。 So in this example we have put a function inside these brackets, so an anonymous function will be created inside memory and it object will be returned here. 因此,在此示例中,我们在这些括号内放置了一个function ,因此将在内存中创建一个匿名函数,并将在此处返回其对象。

Now we have put one more pair of brackets after function declaration. 现在,在函数声明之后,我们又放了一对括号。 So, what happens here is (returned function object)() . 因此,这里发生的是(returned function object)() So this function will be called immediately and an object containing two properties funcX and funcY whose value are function will be returned. 因此,将立即调用此函数,并且将返回包含两个属性funcXfuncY其值为函数)的对象。

Now moving to second part that is inside the function. 现在转到函数内部的第二部分。 Here the difference between funcA and funcB is that funcA is same as someModule ie it is also an IIFE and depending upon the body of funcA , it will be decided that if we are returning function object from inside the body of funcA , then it will also be a function otherwise it will contain whatever we return from inside of body of funcA . 这里的区别funcAfuncB是, funcA是相同someModule即它也是一个IIFE并根据身体funcA ,它会决定,如果我们从身体内部返回函数对象funcA ,那么它也将是一个函数,否则它将包含我们从funcA体内返回的funcA

Apart from that all other are simple functions. 除此之外,其他所有功能都很简单。

Also, not related to what was asked. 另外,与要求的内容无关。 We can access funcA and funcB inside funcX and funcY , and even after containing function is executed then also we will still be able to access both of these function. 我们可以访问funcAfuncBfuncXfuncY ,并执行然后我们也仍然能够访问这些功能都包含连功能之后。 For more details on this behavior look for closure in JavaScript. 有关此行为的更多详细信息,请查找JavaScript中的闭包。 Happy learning JavaScript. 快乐学习JavaScript。 :) :)

This is simply a module pattern. 这只是一个模块模式。 The way this is written is very similar to the revealing module pattern . 编写方式与显示模块模式非常相似。

Basically, you the function returns something (anything, really) which is meant to be the public interface to the module (note that by interface I do not mean one that needs to be implemented, merely that this is the endpoint to interact with the module, like an API). 基本上,该函数返回的东西(实际上是任何东西)实际上是模块的公共接口(请注意,通过接口我并不意味着需要实现该接口,仅是这是与模块进行交互的端点,例如API)。

The classical way to do this (as a revealing module pattern) would actually be: 做到这一点的经典方法(作为显示模块模式)实际上是:

var someModule = (function() {
    var funcA = (function() {...})();
    var funcB = function() {...};
    var funcX = function(){...}
    var funcY = function(){..}
    return {
        funcX: funcX,
        funcY: funcY
    };
})();

The reason this is called the revealing module pattern, is because you define your module and all private and public functions in the module, and in the end you reveal what you want to expose. 之所以称为显示模块模式,是因为您定义了模块以及该模块中的所有私有和公共功能,最后您还揭示了要公开的内容。

This makes it easy to understand what's happening inside and to change it if necessary. 这样可以轻松了解内部发生的情况,并在必要时进行更改。

A good resource to quickly learn about various module patterns is this site 该站点是快速了解各种模块模式的好资源

Andrew, 安德鲁,

So it's a little confusing, but it's pretty basic javascript weirdness. 因此,这有点令人困惑,但这是非常基本的javascript怪异。

You can save a function in a variable like this: 您可以将函数保存在这样的变量中:

var a = function(){return 30}

Here a hold the function itself. 这里a函数本身。 You can then call this function with (): 然后,您可以使用()调用此函数:

a() // result is 30;

Alternatively I could forget the variable and just call the function by adding the () to the end: 或者,我可以忘记变量,而只需在末尾添加()来调用函数:

(function(){return 30})();

this also results in 30, although we didn't assign it to anything. 尽管我们没有将其分配给任何对象,但这也导致了30。

So now if I assign that last one to a variable: 现在,如果我将最后一个赋值给变量:

var b = (function(){return 30})();

Unlike a above, which holds the function itself, b holds the result, whatever that might be, of calling the function — b is 30 because the function is called and then the result is assigned to b. 不像a上面,它拥有本身的功能,B保存结果,不管这可能是中,调用函数- b为30,因为函数被调用, 然后将结果分配给b。

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

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