变量与参数范围和赋值 [英] Variable vs Parameter Scope and Assignment Values

查看:28
本文介绍了变量与参数范围和赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看道格拉斯·克罗克福德(Douglas Crockford)的视频系列.我对分配变量作为全局变量分配参数的关系有些困惑,让我演示一下.

I have been watching a video series by Douglas Crockford. I am a little confused on the relationship of assigning variables as a parameter of assigning variables as a global variable, let me demonstrate.

var bob = 22; //global scope
function funky(parameter){
    parameter = null; //sets the var passed in to null
    bob = 44; //sets the bob 
}

var hello = [];
funky(hello); // hello still has the value of an empty array even after the function is invoked.
bob; // value is 44

因此,当调用funky()时,将为'var bob'分配44.该鲍勃将新值保存在函数范围之外.

So 'var bob' is assigned 44 when funky() is invoked. This bob holds the new value outside the scope of the function.

var hello作为参数传递,而在funky内部,它的值为null;在全局空间中调用hello时,外部的值为空.

var hello is passed as a parameter, while inside funky, it has the value null, outside when hello is invoked in global space, it holds the value of an empty array.

这只是我要记住的吗?如果将全局范围内的已定义变量作为参数传递,它将仅将其新分配的值保存在函数范围内吗?我是否在更广泛的范围内缺少关于如何在函数中传递和分配参数的东西?

Is this just something I have to memorize? If a defined variable in global scope is passed as a parameter, it will only hold it's new assigned value within the scope of the function? Am I missing something at a broader scope with either how a parameter is passed and assigned in a function?

这是功能/全局范围内外的带有console.log输出的代码的副本.

Here is a repl.it of the code with console.log outputs while inside and outside the function/global scope.

http://repl.it/NgN

推荐答案

因为这一行

parameter = null;

仅设置函数的参数的值,而不设置传入的变量的值.但是,函数内部的 bob 被解释为对全局变量,因此它确实会修改外部值.

Only sets the value of the function's parameter, not the value of the variable that was passed in. However, bob inside the function is interpretted as a reference to the global variable, so it does modify the external value.

但是请注意,对象是引用,因此,如果您这样编写它:

Note, however, that objects are references, so if you wrote it like this:

function funky(parameter){
    parameter.hello = null;
}

然后调用 funky 会直接修改参数引用的对象.

Then calling funky does directly modify the object that the parameter references.

var x = { hello: [] };
funky(x); // x => { hello: null }

这只是我要记住的吗?

Is this just something I have to memorize?

是的,了解如何传递参数非常重要.

Yes, understanding how parameters are passed is pretty important.

如果将全局范围内的已定义变量作为参数传递,它将仅将其新分配的值保存在函数范围内吗?

If a defined variable in global scope is passed as a parameter, it will only hold it's new assigned value within the scope of the function?

参数仅在函数范围内保留其值.全局变量是全局变量,因此,如果在函数内部对其进行修改,则它们会将值保留在函数外部.

Parameters only hold their values within the scope of the function. Global variables are, well, global, so if they are modified inside the function, they keep that value outside the function.

此外,请注意隐藏—如果参数与全局变量具有相同的名称,则该参数会将变量隐藏在该函数的范围内.

Also, be aware of hiding—if a parameter has the same name as a global variable, the parameter hides the variable within the scope of that function.

这篇关于变量与参数范围和赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆