JavaScript 对象作为函数参数 [英] JavaScript objects as function parameters

查看:76
本文介绍了JavaScript 对象作为函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 JavaScript,假设我有一个函数 X,并在该函数中创建了一个名为 objectX 的对象.函数 X 返回 objectX.后面的代码函数 Z(somevar, anObject) 接收 objectX 作为它的参数之一.

Using JavaScript, say I have a function X, and in that function an object called objectX is created. function X returns objectX. Later in the code function Z(somevar, anObject) receives objectX as one of it's parameters.

现在在函数 Z 中,objectX 及其所有属性在函数 Z 中都被称为 anObject 吗?

Now in function Z, is objectX and all its properties referred to as anObject inside function Z?

如果函数 Z 返回一个对象会发生什么?其余的代码会将对象视为objectX"还是anObject"?

And what happens if function Z returns anObject? Will the rest of the code see the object as "objectX" or "anObject"?

function X() {
    ...
    objectX = {};
    ...
    return objectX;
}

X();

function Z(anything, anObject) {
    ...
    return anObject
}

Z(something, objectX);

推荐答案

Javascript 具有 功能范围.这意味着在函数内声明的每个变量只能从该函数内访问.

Javascript has function scope. This means that every variable declared within a function, will only be accessible from within that function.

如果您使用 var 正确声明了 objectX 变量,如下所示:

If you’d properly declared the objectX variable with var, as follows:

function X() {
    ...
    var objectX = {};
    ...
    return objectX;
}

那么 objectXX 函数中只会被称为 objectX.在其他地方,它将被称为您分配给它的任何变量.由于在您的代码中,您没有将 X() 的结果分配给任何东西,objectX 将无法从任何地方访问.

then objectX would only be known as objectX inside the X function. Elsewhere, it would be known as whatever variable you’d assigned it to. Since in your code, you don’t assign the result of X() to anything, objectX would not be accessible from anywhere.

然而,这是 Javascript 更严重的设计缺陷之一:如果您显式声明变量(使用 var 语句,或作为函数参数),该变量将自动成为全局变量.这意味着它可以任何地方访问.

However, here’s one of Javascript’s more serious design flaws: if you don’t explicitly declare a variable (using the var statement, or as a function parameter), that variable will automatically become a global variable. That means that it will be accessible anywhere.

因此,在上面的代码中,您可以通过该名称在任何地方访问 objectX.

Because of this, in your code above, you can access objectX everywhere by that name.

anObject,另一方面,正确声明的(作为参数),这意味着它的范围将被限制为 Z 功能.

anObject, on the other hand, is properly declared (as a parameter), and that means its scope will be limited to the Z function.

简而言之,按照您的代码编写方式,objectX 可以通过 objectX 变量在任何地方访问,并且在函数 Z 内部,您可以将其引用为 objectXanObject.

In short, the way your code is written, objectX is accessible everywhere by way of the objectX variable, and inside the function Z, you can reference it both as objectX and as anObject.

但是请注意,全局变量是坏事™,因为它们会让您很难弄清楚由谁、何时以及为什么分配了哪些变量——正如您所注意到的.
虽然 Javascript 无法完全避免它们,但作为一项规则,您应该尽量保持变量的范围尽可能小(范围 = 在程序中可以访问该变量的位置).

Do note, however, that global variables are a Bad Thing™, since they can make it quite hard to figure out what variable gets assigned by who, when, and why — as you’ve noticed.
While Javascript makes it impossible to completely avoid them, as a rule you should try to keep the scope of your variables as small as possible (scope = where in your program that variable can be accessed).

为此,我建议重构您的代码,如 igorw 有.

To that end, I would recommend refactoring your code like igorw has.

这篇关于JavaScript 对象作为函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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