Created
May 21, 2011 07:09
-
-
Save bebraw/984325 to your computer and use it in GitHub Desktop.
"this" question material (JSShaper).
This file contains 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
// current result | |
var Button = new Class({ | |
initialize: function() { | |
this.color = 'blue'; | |
this.on('click', function() { | |
that.color = 'red'; | |
}) | |
} | |
}); | |
// desired result | |
var Button = new Class({ | |
initialize: function() { | |
var that = this; | |
this.color = 'blue'; | |
this.on('click', function() { | |
that.color = 'red'; | |
}) | |
} | |
}); |
This file contains 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
// current input | |
var Button = new Class({ | |
initialize: function() { | |
this.color = 'blue'; | |
this.on('click', function() { | |
_.color = 'red'; | |
}) | |
} | |
}); | |
// desired input | |
var Button = new Class({ | |
initialize: function() { | |
this.color = 'blue'; | |
this.on('click', function() { | |
_color = 'red'; | |
}) | |
} | |
}); |
This file contains 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
"use strict"; "use restrict"; | |
var Shaper = Shaper || require("shaper.js") || Shaper; | |
Shaper("this", function(root) { | |
return Shaper.traverseTree(root, { | |
pre: function(node, ref) { | |
var prefix = '_.'; | |
var childThisName = 'that'; | |
var expr = Shaper.parseExpression(prefix + '$'); | |
if(Shaper.match(expr, node)) { | |
var thisExpr = Shaper.parseExpression(childThisName + '.$'); | |
Shaper.replace(thisExpr, node.children[1]); | |
Shaper.cloneComments(thisExpr, node); | |
return ref.set(thisExpr); | |
} | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment