Influenced by jQuery, I’m experimenting with method chaining in javascript. I constructed a wrapper around an array which will accept coordinate points and transform methods. It’s general syntax is something like:
myPath = new Path().move({x:50,y:50}).line({x:20,y:20}).rotate(Math.PI/3);
it works well, and it makes it somewhat more readable than a string of coordinates. However now I want to be able to duplicate the existing path by doing a concatenation of
its reversed self:
// create a symmetrical path.
myPath = new Path().move().line().etc().etc.concat(myPath.reverse());
But that fails because myPath is unknown as argument to concat. It works when i do:
var myPath = new Path();
myPath.move().line().etc().etc().concat(myPath.reverse());
But I’m wondering is there a construct other and shorter than above to immediately assign the new Object to the variable definition?
If its not possible in Javascript, i’d be interested if it’s possible in other languages?
regards,
Jeroen.
Not very elegant, but here you go.
No, you can’t reference what hasn’t been created yet.
You could do this as well: