简体   繁体   English

如何使这个简单的javascript函数?

[英]How do I make this simple javascript function?

I have three variables 我有三个变量

var r1 = 12;
var r2 = '';
var r3;

I need to make a function convert() with three parameters as so: 我需要使用以下三个参数制作一个convert()函数:

function convert(arg1, arg2, arg3) {
    // function body here
}

such that after I run this code 这样我运行这段代码后

convert(r1, r2, r3)
console.log(r1, r2, r3)

I should get the output: 我应该得到输出:

r2, "empty string", "undefined value"

That is it should changed the values of r1, r2 and r3 to the above values. 也就是说,应将r1,r2和r3的值更改为上述值。

Code I tried: 我试过的代码:

function convert(arg1, arg2, arg3) {
  if (arg2 === '') {
    arg2 = "empty string";
  }
  if (arg3 === undefined) {
    arg3 = "undefined value";
  }
}

and then call convert(r1, r2, r3) it fails ( obviously! ) because of arguments are passed as values. 然后调用convert(r1, r2, r3)失败( 很明显! ),因为参数作为值传递。 But I need to know is there any way to make this function? 但是我需要知道有什么方法可以实现此功能吗? Also, I have seen this question , so I know about how they are passed, but still, is there any way I can make this work? 另外,我已经看到了这个问题 ,所以我知道它们是如何通过的,但是,仍然有什么方法可以使我工作吗?

You can't do this in JavaScript. 您无法在JavaScript中执行此操作。 It doesn't have pass-by-reference semantics. 它没有按引用传递语义。

You can use an intermediate object fo this, but seems that's not what you are looking for: 您可以为此使用一个中间对象,但是这似乎并不是您想要的:

var data = {r1: 12, r2: '', r3: undefined};
function convert(data, field1, field2, field3) {
    // …
}

You could do something like this, but most likely something is flawed with your logic. 您可以执行类似的操作,但是很可能您的逻辑存在某些缺陷。

function convert(arr) {
    for (var i = 0, l = arr.length; i < l; i++) {
        // do your stuff here on arr[i]
    }
    return arr;
}

var a = convert([r1, r2, r3]);
//a now holds the right values, do whatever you want with them
//you can even load them back to the variables if that's what you really want
//r1 = a[0]; etc.

I don't know what is the reason to do this, where you want to use these new values, but it would be more clever to write the convert function for only one value, and call it when it's necessary. 我不知道这样做的原因是什么,您想在哪里使用这些新值,但是只为一个值编写convert函数并在必要时调用它会更聪明。

function convert(val) {
    if (val === '') {
        return 'empty string';
    }
    if (typeof val === 'undefined') {
        return 'undefined value';
    }
    return val;
}

console.log(convert(r1));

In the unlikely event that you only want to convert global variables, you can just pass in the names of those variables: 万一您只想转换全局变量,只需输入这些变量的名称即可:

function convert(arg1, arg2, arg3) {
  if (window[arg2] === '') {
    window[arg2] = "empty string";
  }
  if (window[arg3] === undefined) {
    window[arg3] = "undefined value";
  }
}

var r1 = 12;
var r2 = '';
var r3;
convert('r1', 'r2', 'r3');
console.log(r1, r2, r3);

working code: fire the execute function on the event you need 工作代码:在您需要的事件上触发执行函数


function execute(){
    var r1 = 12;
    var r2 = '';
    var r3;
    convert(r1,r2,r3);
}

function convert(arg1, arg2, arg3) {
  if (arg2 === '') {
    arg2 = "empty string";
  }
  if (arg3 === undefined) {
    arg3 = "undefined value";
  }

  console.log(arg1,arg2,arg3);
//    console.log(arg2);
  //    console.log(arg3);

}

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

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