Skip to content

Instantly share code, notes, and snippets.

@ben-ng
Last active March 24, 2017 18:20
Show Gist options
  • Save ben-ng/d2643db8a6fdda23fd6b0fa787971236 to your computer and use it in GitHub Desktop.
Save ben-ng/d2643db8a6fdda23fd6b0fa787971236 to your computer and use it in GitHub Desktop.
Salesforce Marketing Cloud closure scope bug
function foo() {
function boom() {
Write('Boom');
};
function bar () {
function baz() {
boom(); // Fails here, "boom" can't be found
};
return baz;
};
return bar();
}
foo()();
// Where you execute a closure changes its scope!
function foo() {
function boom() {
Write('Boom');
};
function bar () {
function baz() {
boom();
};
return baz;
};
return bar()(); // to here, the problem goes away!
}
foo(); // If you move the second application from here...
function foo() {
function boom() {
Write('Boom');
};
function bar () {
var boom2 = boom; // Bunny-hop to a new variable. `boom = boom` does not work.
function baz() {
boom2();
};
return baz;
};
return bar();
}
foo()();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment