Skip to content

Instantly share code, notes, and snippets.

@AmirHossein
Created January 29, 2013 13:52
Show Gist options
  • Save AmirHossein/4664391 to your computer and use it in GitHub Desktop.
Save AmirHossein/4664391 to your computer and use it in GitHub Desktop.
Call string functions in MooTools
String.implement({
attempt: function() {
var parts = this.split('.'), len = parts.length;
var func = window, bind;
for (var i=0; i<len; i++) {
if (func.hasOwnProperty(parts[i])) {
bind = func;
func = func[parts[i]];
}
}
if (typeOf(func) == 'function') {
return func.apply(bind, arguments);
}
return null;
}
});
/* Usage */
obj1 = {
obj2: {
obj3: {
func: function(a, b) {
return a + b;
}
}
}
}
alert( 'obj1.obj2.obj3.func'.attempt(1, 2) ); // "3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment