JavaScript: Define variable name with function parameter - Stack Overflow

Is it possible in JavaScript to define a variable name with a function parameter?Here is my jsFiddle. I

Is it possible in JavaScript to define a variable name with a function parameter?

Here is my jsFiddle. In the example below, I would like varB to be defined as null when it is passed through makeNull(varName):

var varA = undefined;
if (varA == undefined) {
  varA = null;
}
var varB = undefined;

makeNull(varB);

function makeNull(varName) {
  if (varName == undefined) {
    varName = null;   
  }
}  

alert (varA + ' | ' + varB);

Is it possible in JavaScript to define a variable name with a function parameter?

Here is my jsFiddle. In the example below, I would like varB to be defined as null when it is passed through makeNull(varName):

var varA = undefined;
if (varA == undefined) {
  varA = null;
}
var varB = undefined;

makeNull(varB);

function makeNull(varName) {
  if (varName == undefined) {
    varName = null;   
  }
}  

alert (varA + ' | ' + varB);

Share Improve this question asked Oct 2, 2012 at 14:59 Mark RummelMark Rummel 2,95010 gold badges38 silver badges52 bronze badges 4
  • You appear to be trying to break encapsulation by changing the bindings in another higher scope. Why? Even if there is a solution, this isn't good software practice. – Francis Avila Commented Oct 2, 2012 at 15:01
  • I agree with Francis, but there's nothing wrong with creating functions to modify objects or variables. You should just be explicit about what you pass in, and what you return. Maybe something like this? jsfiddle/zVM9M/2 – Daniel Attfield Commented Oct 2, 2012 at 15:02
  • @FrancisAvila I'm getting the value of multiple checkboxes via jQuery. The ones that are checked = 1 the ones that aren't checked are undefined, but I need them to be null. For example: checkbox1 = 1, checkbox2 = 1, checkbox3 = null. – Mark Rummel Commented Oct 2, 2012 at 15:21
  • 1 If you represent all the checkboxes as an array, you can pass the entire array to your makeNull function, and it can set the undefined elements to null. – Barmar Commented Oct 2, 2012 at 16:27
Add a ment  | 

3 Answers 3

Reset to default 2

If you are trying to define a variable with a function parameter then you either want to use an array or an object.

JavaScript doesn't provide "call by reference" parameters. When you call a function, it receives the values of the argument expressions, not references to the variables that were in the expressions. So it can't modify the bindings of those variables.

If you want to do something like this, you can use containers and labels, e.g.

function makeNull(obj, label) {
  if (obj[label] === undefined) {
      obj[label] = null;
  }
}
var varB = { abc: undefined }
makeNull(varB, 'abc');

you can pass a variable (defined as 'undefined') to a function and set its value.

http://jsfiddle/AwnVN/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745245905a4618409.html

相关推荐

  • JavaScript: Define variable name with function parameter - Stack Overflow

    Is it possible in JavaScript to define a variable name with a function parameter?Here is my jsFiddle. I

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信