方法内的局部变量,并分配一个函数?

时间:2021-08-08 19:42:29

I came across some codes that for me it looked confusing, although I looked in many places googling, I could not find the exact answer for my question. In hope to know what's going on with this code I'm here, asking if there's a good explanation for me, so I would not only know how to do it but, learn what it does as well!

我遇到了一些代码对我来说看起来很混乱,虽然我在很多地方搜索谷歌搜索,我找不到我的问题的确切答案。希望知道这个代码发生了什么,我在这里,问我是否有一个很好的解释,所以我不仅知道该怎么做,而且还要了解它的作用!

var fun = {
  a_method: function(){
    var f = function(){
      alert("FUN FUNNY FUNCTION");
    };
    f.another_method = function(){ // in this line, is another_method local or not? Because like mentioned f is local; how about another_method after the dot?
      alert("this is another method inside my fun method");
    };
    return f; // this is a local variable, why another_method isn't local?
  }() // these parentheses, what do they do in regards to the function?
};

What does return f do? Why does f.another_method(){function(){}} become fun.a_method.another_method() <= these parentheses confuse me because if f is local inside a_method, likewise shouldn't everything else be local as well? Like f.another_method(){function(){...}}, or to even be clearer I want to know why the .another_method() with an f preceding the function(before the dot) return f. and goes passed to another_method()? To be called fun.a_method.another_method(); and no f in there see where the confusion is.

返回f做什么?为什么f.another_method(){function(){}}变得有趣.a_method.another_method()<=这些括号让我感到困惑,因为如果f在a_method中是本地的,那么其他一切也不应该是本地的吗?就像f.another_method(){function(){...}}一样,或者甚至更清楚我想知道为什么.another_method()在函数前面(在点之前)返回f。并传递给another_method()?被称为fun.a_method.another_method();并且没有f在那里看到混乱的地方。

fun.a_method.another_method() <= these parentheses confuse me f.another_method(){function(){}};

fun.a_method.another_method()<=这些括号让我感到困惑f.another_method(){function(){}};

I should call it this way, but I'm not right (why, just because the variable is local) and this is not how it works with JS:

我应该这样称呼它,但我不对(为什么,因为变量是本地的),这不是它如何与JS一起工作:

fun.a_method.f.another_method();

Trying to understand why not the above instead:

试着理解为什么不是上面的代替:

fun.a_method.another_method(); // note that it is without the "f" when it's called, now why not or "how it became this way"?

1 个解决方案

#1


0  

var anonymous = function(){var f = function(){alert()}; return f}();anonymous();
var anonimo = {method:function(){var f = function(){alert("this is an anonimous function")}; return f;}()}
anonimo.method();
var why = {method:function(){
var f = function(){
alert("this an anonymous function because the function was declared from a variable, is this right?")
};
f.notHere = function(){
alert("correct me if I'm wrong but, f is variable declared to an anonymous function just f alone \"notHere\"")
};
return f
}()};
why.method.notHere();
var lastScenario = {
method:function(){
var f = function(){};
f.notHere=function(){
alert("this will not work if I don't declare f as a function")
};return f}()};
lastScenario.method.notHere();

#1


0  

var anonymous = function(){var f = function(){alert()}; return f}();anonymous();
var anonimo = {method:function(){var f = function(){alert("this is an anonimous function")}; return f;}()}
anonimo.method();
var why = {method:function(){
var f = function(){
alert("this an anonymous function because the function was declared from a variable, is this right?")
};
f.notHere = function(){
alert("correct me if I'm wrong but, f is variable declared to an anonymous function just f alone \"notHere\"")
};
return f
}()};
why.method.notHere();
var lastScenario = {
method:function(){
var f = function(){};
f.notHere=function(){
alert("this will not work if I don't declare f as a function")
};return f}()};
lastScenario.method.notHere();

相关文章