I want to check out whether the arguments provided in a function are strings and for this I'm using the following condition:
function someFunction (variable1, variable2, variable3) {
["variable1", "variable2", "variable3"].forEach(function (each) {
if (!(/*something*/[each].constructor.name === "String")) throw new TypeError(
each + " must be a string. " + /*something*/[each].constructor.name +
" was given instead."
);
else ...
});
}
Had the check occurred in the global namespace, I could've used window[each], since variables are properties of window, like below:
var variable1, variable2, variable3;
["variable1", "variable2", "variable3"].forEach(function (each) {
if (!(window[each] instanceof String)) throw new TypeError(
each + " must be a string. " + window[each].constructor.name + " was given instead."
);
else ...
});
How can the above be achieve inside a function?
window[name]can only be used with global variables, there's nothing equivalent for local variables.