Rick Waldron (2013-12-19T22:34:30.000Z)
domenic at domenicdenicola.com (2014-01-06T13:47:04.895Z)
On Thu, Dec 19, 2013 at 3:03 PM, Andrea Giammarchi <andrea.giammarchi at gmail.com> wrote: > It seems that I need to create N amount of garbage by design. > > This does not work, the const has already been defined: > > ```javascript > try { > new Proxy({},{}); > const ES6_PROXY = true; > } catch(o_O) { > const ES6_PROXY = false; > } > ``` > That doesn't work anyway, not because the const has already been defined, but because ES6_PROXY is defined within block bodies. Same as: ```js { const IS_BOUND_TO_THE_BLOCK = true; } ``` > This does not work neither > > ```javascript > try { > new Proxy({},{}); > var ES6_PROXY = true; > } catch(o_O) { > var ES6_PROXY = false; > } > const ES6_PROXY = false; > > // var 'ES6_PROXY' has already been declared > ``` > Because the var was hoisted up to the const's scope and const can't be used to redeclare an existing binding of the same name. Is ES6_PROXY meant to be bound in the global scope? > neither does the following > > ```javascript > try { > new Proxy({},{}); > let ES6_PROXY = true; > } catch(o_O) { > let ES6_PROXY = false; > } > > // Illegal let declaration outside extended mode > ``` That's a Canary-specific error, but the code wouldn't do what you want anyway, for the same reason as the first example. > As summary, there is no way to feature detect and define a const in the > same scope, a closure without the possibility to define such constant as > well is mandatory. > > ```javascript > const ES6_PROXY = function(){ > try { > new Proxy({},{}); > return true; > } catch(o_O) { > return false; > } > }(); > ``` > This works fine: ```js var result = true; try { new Proxy({},{}); } catch(o_O) { result = false; } const ES6_PROXY = result; ```