简体   繁体   English

带有可变数量参数的Javascript函数-更改其值-然后返回

[英]Javascript function that takes variable number of arguments - changes their value - and then returns them

I am new to Javascript (and programming in general) and have been searching for a way to change the value of an arbitrary number of aguments using a Javascript function. 我是Java语言(和一般编程领域)的新手,并且一直在寻找一种使用Javascript函数更改任意数量参数的值的方法。

The answer here ( JavaScript variable number of arguments to function ) was quite helpful. 答案( JavaScript函数变量的数量可变 )非常有用。 I was able to use it to create the two of the functions I need, but I'm having trouble with the third. 我能够使用它来创建所需的两个功能,但是我在使用第三个功能时遇到了麻烦。

Basically I would like to pass a variable number of objects (primitive or more complex ) into a function and have the function change the value of each object. 基本上,我想将可变数量的对象(原始或更复杂的对象)传递给函数,并让该函数更改每个对象的值。

var pants = true;
var house = true;
var hair = {/* lots of stuff */};

var onFire = function() {
        for (var i = 0; i < arguments.length; i++) {
            arguments[i] = false;
        }
};

onFire(pants, house, hair);

Console outputs: 控制台输出:

>pants;
 true

>house;
 true

>hair;
 Object

How can I formulate this function so the the result is: 我如何公式化此函数,所以结果是:

>pants;
 false

>house;
 false

>hair;
 false

Any help would be greatly appreciated! 任何帮助将不胜感激!

Edit: 编辑:

To clarify things - I am trying to create a reusable helper function that changes the value of any object passed in to false instead of typing: 为了澄清问题-我试图创建一个可重用的帮助器函数,该函数将传入的任何对象的值更改为false而不是键入:

var a1 = false;
var a2 = false;
...
var a+n = false;

If I use mathec 's method - is it possible to 'parse' object so that it's properties overwrite the global variables of the same name, or am I stuck with typing it out explicitly each time? 如果我使用mathec的方法-是否可以“解析” object以便其属性覆盖相同名称的全局变量,还是我每次都明确地键入它?

var object = {
    pants: {/* lots of stuff */},
    house: {/* lots of stuff */},
    hair: {/* lots of stuff */}
};

function modifyObject(obj) {
    obj.pants = false;
    obj.house = false;
    obj.hair = false;
}

function someMagic(object){
    // wand waves...
    // rabbit exits hat....
}

Console Output: 控制台输出:

>pants;
 false

>house;
 false

>hair;
 false 

When you pass variables in JavaScript you're passing a copy of a reference to a value if it's an object, but if it's a primitive (like a true/false) you're copying the actual value. 当您在JavaScript中传递变量时,如果它是一个对象,则传递的是对值的引用的副本,但是如果它是基元(如真/假),则是在复制实际值。 This basically means, if you pass in an object and modify one of the object's properties, you'll be modifying the original object. 这基本上意味着,如果传入一个对象并修改该对象的属性之一,则将修改原始对象。 But if you pass in a primitive like true/false, you'll just be modifying the copy, and not the original value. 但是,如果传入诸如true / false之类的原语,则只会修改副本,而不是原始值。 So if your goal is to modify the original values, one option is to store those values in an object and pass in the object instead. 因此,如果您的目标是修改原始值,则一种选择是将这些值存储在对象中,然后传递给该对象。 For example: 例如:

var object = {
    pants: true,
    house: true,
    hair: {}
};

function modifyObject(obj) {
    obj.pants = true;
    obj.house = true;
    obj.hair = true;
}

If you want to modify an arbitrary number of arguments, just remember that if you're passing in true/false you're copying those values. 如果要修改任意数量的参数,请记住,如果传递的是true / false,则将复制这些值。 So if you change them inside the function you won't be modifying the original values. 因此,如果您在函数中更改它们,则不会修改原始值。

A quick way to remember this is if you ever pass an object or an array, you're modifying the actual object. 记住这一点的一种快速方法是,如果您曾经传递对象或数组,则是在修改实际的对象。 Anything else, you're modifying a copy. 除此之外,您正在修改副本。

Edit 编辑

So interpreting what you want to do literally, you could write this: 因此,按字面意义解释您要执行的操作,可以这样编写:

var a = true,
    b = true;

/* pass in variable names */
function makeFalse() {
   var i, len;

   for (i = 0, len = arguments.length; i < len; ++i) {
     window[arguments[i]] = false;
   }
 }

 makeFalse("a", "b");

But I can't think of a good reason to do this :-). 但我想不出这样做的充分理由:-)。 I would use configuration objects to store flags and state variables, not global variables. 我将使用配置对象存储标志和状态变量,而不是全局变量。

Unfortunately, this is not directly possible. 不幸的是,这不是直接可能的。

Javascript is a pass-by-value language, and in the case of object types, its a pass by value by reference (at least thats how Ive seen it referred to) Javascript是一种按值传递的语言,对于对象类型,它是按引用传递值的(至少就是我看过它所引用的方式)

Is JavaScript a pass-by-reference or pass-by-value language? JavaScript是按引用传递还是按值传递语言?

goes pretty in depth to it, and https://stackoverflow.com/a/3638034/1093982 in particular has a great answer. 非常深入,特别是https://stackoverflow.com/a/3638034/1093982有一个很好的答案。

here is a jsfiddle demonstrating the example in the above answer: 这是一个jsfiddle,展示了上述答案中的示例:

http://jsfiddle.net/hjPHJ/ http://jsfiddle.net/hjPHJ/

note how anything assigning to a parameter is not carried into the scope above. 请注意,分配给参数的任何内容如何包含在上述范围内。

You could try JavaScript's typeof() function, which should return "object", and then treat it differently in your code. 您可以尝试JavaScript的typeof()函数,该函数应返回“对象”,然后在代码中对其进行不同的处理。 As for changing the value of an object, I think you actually want to change the value of a property, but I am not sure I understand your question well enough. 至于更改对象的值,我认为您实际上是想更改属性的值,但是我不确定我是否足够理解您的问题。

To change an object property, you would do something like this. 要更改对象属性,您可以执行以下操作。

hair.color = 'red';

If you want to unset the object, you can do this. 如果要取消设置对象,可以执行此操作。

delete window.some_var; 

Javascript passes each argument as a reference by value, so when you change the value of a passed in reference it modifies the copied value. Javascript通过值将每个参数作为引用传递,因此,当您更改传入的引用的值时,它将修改复制的值。

However it does have something else which is very powerful and thats closure. 但是,它确实还有其他功能非常强大,那就是闭包。 In your example onFire already has access to pants , house , and hair through the closure scope and can modify them directly. 在您的示例中, onFire已经可以通过封闭范围访问pantshousehair ,并可以直接对其进行修改。

When you call a function in javascript, you are passing values and not references to the original object. 当您使用javascript调用函数时,您正在传递值,而不是对原始对象的引用。 There are a couple of ways that you can do what you want, but not directly by passing each value to change to the function. 您可以通过两种方法来执行所需的操作,但不能直接通过将每个值传递给函数来进行更改。

First you can pass an array: 首先,您可以传递一个数组:

function onFire(arr) {
    for (var i = 0; i < arr.length; i++) {
        arr[i] = false;
    }
}

arr = [true, true, {}];
onFire(arr); // arr[0] through arr[2] are now false

Another would be to pass an object, whose properties can be modified, you just can't change the value of the variable you pass as an argument: 另一个方法是传递一个对象,该对象的属性可以修改,只是不能更改作为参数传递的变量的值:

function onFire(obj) {
    obj.pants = false;
    obj.house = false;
    obj.hair = false;
}

onFire( { pants: true, house: true, hair: { } } );
// obj.pants, obj.house and obj.hair are now false

Neither of these accomplishes just what you want, but if I knew the reason for doing what you are asking there might be a better way... 这些都不能满足您的要求,但是如果我知道做您要问的原因,可能会有更好的方法...

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

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