Created
January 29, 2013 13:52
-
-
Save AmirHossein/4664391 to your computer and use it in GitHub Desktop.
Call string functions in MooTools
This file contains hidden or 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
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