-4

I read somewhere that it is absolutely impossible to attain a reference to a scope. Are there any hacks to get a reference to a scope and 'trap' it inside a variable?

EDIT:

I know I can acquire the global scope through the window object. I am referring to dynamically creates scopes of functions and so fourth.

isherwood
  • 58,414
  • 16
  • 114
  • 157
chopper draw lion4
  • 12,401
  • 13
  • 53
  • 100
  • 1
    far too vague a question without examples. scope/closure is not a trivial concept – charlietfl Dec 21 '14 at 17:50
  • 1
    Other than a native extension of the JavaScript engines, no. None of the major engines current offer direct exposure of [lexical environments](http://www.ecma-international.org/ecma-262/5.1/#sec-10.2) or [environment records](http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.1) to JavaScript code. – Jonathan Lonowski Dec 21 '14 at 17:52
  • 1
    Related: [Reference a functions local scope as an object](http://stackoverflow.com/questions/2600361/javascript-reference-a-functions-local-scope-as-an-object) – Jonathan Lonowski Dec 21 '14 at 18:02

1 Answers1

0

Refrence to windows object/global scope inside variable can be like this. Here i assume you are in global scope

 var reftowindows = this;

I am referring to dynamically creates scopes of functions and so fourth.

Function defined at global scope

function returnRefrence(){
var refvar = this;
return this;

}

Calling function return variable holding refrence to window as scope

console.log(returnRefrence()); // console logs "window" object

Calling function with call() and binding it to object changes the scope and returns variable holding refrence to obj as scope

 obj = {};
 console.log(returnRefrence.call(obj)); // console logs "obj" object
A.B
  • 20,110
  • 3
  • 37
  • 71