Hey there! I came across this interesting piece of code...
function a(values) {
for (var key in values) {
if (!values.hasOwnProperty(key)) {
continue;
}
this[key] = values[key];
}
}
a({ 'example': 'value' });
While this code unpacks variables from the object successfully, it attaches them to global scope by default, as this
refers to the window
object in this context.
So, even after the function execution, calling alert(example)
will display value
, which isn't ideal. But how can we ensure that these variables are only scoped within the function itself?