I'm reading a textbook of the JavaScript language. And while I'm studying the closure topic, I came up with the following question.
Considering this function:
function foo() {
extractPropsToCurrentContext({'prop1' : 'hello', 'prop2' : 123});
}
I want the result of the above code, equals to:
function foo() {
var prop1 = 'hello';
var prop2 = 123;
}
So, my question is, how to implement the function extractPropsToCurrentContext(/* Object */) ?
Just to clarify, I want to extract those properties of the object into the execution context, not under the 'this' pointer. (So, those extracted props should be private inside that function).
Another thing to clarify, you can't assume the foo will be invoked with 'new'. (like new foo())
Update:
I mean, is there any possibility that we could use any hacky tricks to detour the restriction of browser, to get closer to the result we want? Like, years ago, we invented JSONP for cross-domain, long-pulling for message pushing, etc?