Created
November 7, 2014 11:08
-
-
Save user24/14908e7fbce0349863be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// It would be very simple to write this function to sort-of mimic let; | |
lett({ | |
'blockScopeVar': 4 | |
}, function (blockScopeVar) { | |
//blockScopeVar === 4 // true | |
}); | |
// Then when you want to change the code to actually use let, it's fairly easy; | |
// just write your let declarations and rip out the callback's innards; | |
let blockScopeVar = 4; | |
//blockScopeVar === 4 // true |
Why not just?
(function(blockScopeVar){
//blockScopeVar === 4 // true
})(4);
-
While
Object.keys(..)
tends to be somewhat predictable with ordering the same as you listed them, it's not at all guaranteed. So, if it listed the variables in a different order than what you put into the object literal, you'd have an ordering-mismatch with your parameters. Since it's not reliable, I don't think I'd trust it. -
Wrapping a function around any arbitrary code (either my style or yours) isn't necessarily as "safe" as normal blocks w/
let
, because functions change the meaning ofthis
,return
,break
,continue
,yield
, etc.Where
{ let x = 2; return y * x; }
is perfectly fine and safe,lett({ x: 2 },function(x){ return y * x; })
wouldn't work. :( -
There are tools for transpiling
let
into pre-ES6 browsers. For example:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
so lett would look something like this:
function lett(vars, callback) {
var valuesArray = Object.keys(vars).map(function (varName) {
return vars[varName];
});
callback.apply(this, valuesArray);
}