This is a SO call
I would like to know how someone will get invalid formal parameters in a function without an object argumentsin order to simulate without knowing the format of the destination to destruct the parameter. This is not an ECMAScript question and applies only to JavaScript.
Your mySolutioncannot access argumentsor test. You are provided with an array argscontaining parameter names. You must return an object that has a property for each parameter, which is the parameter that was passed to the function. In short, results[prop]should === test[prop]. Your decision should not rely on bugs or security holes, as they may be missing in the future. The solution to this problem, which I mean, does not depend on any errors.
(function () {
function mySolution ({
var,
this,
function,
if,
return,
true
}) {
var test = arguments = null,
args = ['var', 'this', 'function', 'if', 'return', 'true'],
results = {};
return results;
};
var test = {
"var" : {},
"this" : {},
"function": {},
"if" : {},
"return" : {},
"true" : {}
},
results = mySolution(test),
pass = true;
for (var prop in test)
if (test.hasOwnProperty(prop))
if (results[prop] !== test[prop])
pass = false;
alert(pass ? "PASS" : "FAIL")
}());
Here is one of two possible solutions that I would make:
(function () {
function mySolution ({
var,
this,
function,
if,
return,
true
}) {
var test = arguments = null,
args = ['var', 'this', 'function', 'if', 'return', 'true'],
results = {};
var i = args.length;
while (i--) {
results[args[i]] = eval("function::" + args[i]);
}
return results;
};
var test = {
"var" : {},
"this" : {},
"function": {},
"if" : {},
"return" : {},
"true" : {}
},
results = mySolution(test),
pass = true;
for (var prop in test)
if (test.hasOwnProperty(prop))
if (results[prop] !== test[prop])
pass = false;
alert(pass ? "PASS" : "FAIL")
}());
The solution works using the default namespace function::in combination with the realm eval().
For example: foo.function::barand foo.function::['bar']is one and the same foo.bar.