1

I have a JS function as below

// A simple array where we keep track of things that are filed.
filed = [];

function fileIt(thing) {
  // Dynamically call the file method of whatever 'thing' was passed in.
  thing.file();

  // Mark as filed
  filed.push(thing);
}

Now, function fileIt(thing) is working well when called as below

fileIt(AuditForm);

Whereas, its giving error at line thing.file(); when i am trying to pass a variable like below

var formID = obj.id;
fileIt(formID);

Variable formID has same value and i.e. "AuditForm" what's wrong here. Kindly suggest.

8
  • It's because obj.id is a string value, whereas when you hard code AuditForm it's a reference to the variable which holds, presumably, an object which has a file() method. There are solutions to this problem, but without a clear example of your code we can't really guide you any further. Commented Apr 30, 2019 at 9:05
  • This is because formID is probably a Number or a string. Numeric/String types have no .file prototype. Commented Apr 30, 2019 at 9:05
  • @RoryMcCrossan Your assumptions are correct so my question here is how can i make formID similar to AuditForm, instead of hardcoding i need to pass it as variable as multiple options may be available Commented Apr 30, 2019 at 9:06
  • "AuditForm" is just a string but AuditForm is an object when used directly. Commented Apr 30, 2019 at 9:06
  • 1
    it's just a name of the object, not the stringified object so no you cannot. however you can create another factory object which have the {name: object} like properties and you can use the same as factoryObject[obj.id] and pass the same in function. Commented Apr 30, 2019 at 9:12

1 Answer 1

1

If obj.id is the string AuditForm, then you have no choice but to use dynamic property notation on the global window object, or use eval if you didn't declare AuditForm with var on the global scope:

If you declare AuditForm with var on the global scope:

fileIt(window[formID]);

If you don't:

fileIt(eval(formID));

Do note that eval is a very poor option, as if obj.id can be interpreted as other code, e.g. another eval call which will be evaluated, then malicious operations can be performed. Example:

const obj = {
  id: "eval('alert(\"Inside an eval script!\")')"
};

eval(obj.id);

Sign up to request clarification or add additional context in comments.

5 Comments

Strongly recommend not suggesting eval to obvious newbies without carefully explaining why not to use it and what to do instead.
Yes @T.J.Crowder - but I don't believe there's another way to do this if you don't declare the variable with var on the global scope without using eval.
@JackBashford I think T.J. Crowder is giving his point with without carefully explaining why not to use it and what to do instead. ... Just explain why it can be used in this context :P.
AuditForm is an object/class not a function so no need of eval so maybe a need of JSON.stringify() or nothing.
@JackBashford - I usually show them what to do instead, e.g., put the things they want to look up this way in an object as properties, and then access the properties via brackets notation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.