简体   繁体   English

在函数中绑定一个变化的变量,我想要一个名字

[英]Bind a changing variable in function, I want a name

I have somethings like: 我有类似的东西:

var i = 0;

var func = function(){
    console.log(i);
};

func(); //0

i++;

func(); //1

I want to have the second console also output ' 0 ', so I change the program like: 我想让第二个控制台也输出“ 0 ”,所以我将程序更改为:

var i = 0;

var func = (function(_i){
    return function(){
        console.log(_i);
    };
})(i);

func(); //0

i++;

func(); //0

I know how it works, but is there any name or terms to describe such mechanism? 我知道它如何工作的,但是是否有任何名称或术语来描述这种机制?

I've been calling this mechanism "breaking the closure" though I've had arguments in the past with people who insist on calling this technique "closure" . 我过去一直将这种机制称为"breaking the closure"尽管我过去一直与坚持将这种技术称为"closure"人们争论不休。

The reason I call it "breaking" closures is because that's what you're doing. 我称其为"breaking"关闭的原因是因为这就是您正在做的。

The classic place where you see this is in solutions for closures in loops: 您看到的经典位置是循环闭包的解决方案:

var hellos = [];

for (var i=0; i < 10; i++) {
    hellos.push(
        (function(j){
            return 'hello ' + j
        })(i)
    );
}

The problem is caused by a closure being created between the outer variable and references to that variable in the inner function (technically the variable is called a "free" variable rather than a closure, "closure" technically refers to the mechanism that captures the variable but in the js community we've ended up calling both things closures). 问题是由于在外部变量和内部函数中对该变量的引用之间创建了一个闭合引起的(技术上,该变量称为“自由”变量而不是闭合,“闭合”在技术上指的是捕获变量的机制但是在js社区中,我们最终将两者都称为闭包。 So closure is the cause of the problem. 因此, 关闭是问题的原因 Since the problem is caused by a closure being created I've started referring to the solution as "breaking the closure" . 由于问题是由创建的闭合引起的,因此我开始将解决方案称为"breaking the closure"

Note that even though some people call this a closure and you may google for "js closure" to read more about this technique it is ironically not a closure. 请注意,即使有人称其为闭包,您也可以在Google上搜索“ js闭包”以了解有关此技术的更多信息,但讽刺的是,它不是闭包。 What it is is simply how functions pass arguments (there's a whole side-argument about how javascript actually pass arguments to functions which you can read here: Why are objects' values captured inside function calls? ). 这仅仅是函数如何传递参数(关于javascript如何实际将参数传递给函数的说法,您可以在这里阅读: 为何在函数调用中捕获对象的值? )。 Javascript is a fairly strict pass-by-value language in the same way C is a strict pass-by-value language (C can ONLY pass by value). Java语言是相当严格的按值传递语言,就像C是严格的按值传递语言(C只能按值传递)一样。

When you pass a reference in js (objects, arrays) the function will not get the original reference but rather a copy of the reference. 当您在js(对象,数组)中传递引用时,该函数将不会获取原始引用,而是获取该引用的副本。 Since it is a reference it obviously points to the same object as the original reference so it is easy to mistakenly believe that javascript passes by reference. 因为它是一个引用,所以它显然指向与原始引用相同的对象,因此很容易错误地认为javascript通过引用传递。 But if you try to assign a new object to the passed-in reference you will notice that the original reference does not change. 但是,如果您尝试将新对象分配给传入的引用,则您会注意到原始引用不会更改。 For example: 例如:

function foo (x) {
    x = [2,3];
}

var y = [1,2];
foo(y);
console.log(y) // prints [1,2] so foo() did not change y

It is this mechanism that is responsible for breaking the association between the outer variable (in your example that would be i ) and the inner variable ( _i in your example). 正是这种机制负责打破外部变量(在您的示例中为i )和内部变量(在您的示例中为_i )之间的关联。 The name of this mechanism is simply function calling (well, technically it is a subset of function calling - it is how arguments are passed to function calls). 这种机制的名称就是简单的函数调用(从技术上讲,它是函数调用的子集-这是将参数传递给函数调用的方式)。

So to recap: 总结一下:

  1. I personally call it "breaking the closure" 我个人称之为"breaking the closure"
  2. Some people call it "closure" even though it is not a closure (the closure is what they want to avoid instead). 即使不是"closure"也有人将其称为"closure" (闭包是他们想要避免的)。

Side note: I realize that the original example is about a global variable but in javascript global variables are just a special case of closures - closures are ALWAYS created when you define a function, it's just that when there's no outer function the outer scope is simply the global scope. 旁注:我意识到原始示例是关于全局变量的,但是在javascript中,全局变量只是闭包的一种特殊情况-闭包总是在定义函数时创建的,只是当没有外部函数时,外部范围就是全球范围。

It's called a closure. 这称为关闭。 You can read more about them here: 您可以在这里阅读有关它们的更多信息:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Closures

When passing in a primitive type variable like a string or a number, the value is passed in by value. 当传入基本类型变量(例如字符串或数字)时,该值将按值传入。 This means that any changes to that variable while in the function are completely separate from anything that happens outside the function. 这意味着在函数中对该变量所做的任何更改都与在函数外部发生的一切完全分开。

if the variable in the scope is not declared, it will search the outer scope, until find it, or to the window scope. 如果未声明作用域中的变量,它将搜索外部作用域,直到找到它为止,或搜索到窗口作用域。 if not find, it will the variable global. 如果找不到,它将是全局变量。

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

相关问题 我想通过在 function 参数中使用变量名存储来使 function 更可重用。 我该怎么做? - I want to make a function more reusable by using a variable name store inside of a function parameter. How would I do that? 我想在.bind函数中添加load事件 - angularJS I want to add load Event at .bind function 我想在更改功能中使用数组名称 - I want to work with array name in change function 我想从函数访问一个变量 - i want to access a variable from function 用名称绑定和取消绑定函数 - bind and unbind function with a name 我想在变量中存储一个 function 并想在 onclick 事件中调用它 - I want to store a function in variable and want to call that at onclick event 为什么不能将变量名作为字符串获取? 我想创建自己的“ console.log”功能 - Why can't I get the variable name as a string? I want to create my own 'console.log' function 当我点击转向按钮时,我希望显示姓名、年龄和位置? 如何访问 function 中的 nextContestant 变量? - When I click on the turn button I want the name, age and location to show up? How to get access to the nextContestant variable in the function? 排序功能正在更改我未排序的变量 - The sort function is changing a variable that i am not sorting 我想将具有相同变量名的 JavaScript 对象组合起来 - I want to combine JavaScript Objects with the same variable name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM