I need to write a function where I can pass it a string and it will replace the word "prefix" in it with the contents of that string. So every time I call the function and pass it a new string i.e. alpha, beta, etc it will completely rebuild all the layers and events inside, using that key as the prefix. I suspect I need to use eval, but I'm not really sure how in this case.
layoutViews = (prefix) ->
prefix_layer1 = new Layer
width: 100
height: 100
prefix_layer1.on Events.Click ->
buttonActions()
layoutViews(alpha)
layoutViews(beta)
I'm using CoffeeScript, but any ideas in real JS are also welcome. I realize this specific question hints at my doing something else wrong in the project, but it's mostly for my own curiosity whether this is even possible.
So you want
function build(prefix) {
window[prefix + "_hi"] = function() {
alert("hi");
};
}
Use like this:
build("test");
test_hi();
Window is just the global (! Bad) element in browsers, so you might consider to assign to this
instead of window
depends on your usage.