Created
June 9, 2009 21:24
-
-
Save swannodette/126802 to your computer and use it in GitHub Desktop.
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
Array.implement({ | |
first: function() { | |
return this[0]; | |
}, | |
rest: function() { | |
return this.slice(1, this.length); | |
}, | |
drop: function(n) { | |
return this.slice(n, this.length); | |
}, | |
isEmpty: function() { | |
return this.length == 0; | |
} | |
}); | |
function $get(first, prop) { | |
var args = $A(arguments); | |
var rest = args.drop(2); | |
var next; | |
if(rest.length == 0) return first[prop]; | |
if(['object', 'array'].contains($type(first))) | |
{ | |
next = first[prop]; | |
} | |
return (next == null) ? null : $get.apply(null, [next].concat(rest)); | |
}; | |
// $get({"foo":["David", 1, 2]}, "foo", 0, 3) -> i | |
// $get({"foo":["David", 1, 2]}, "bar", 0, 3) -> null , note that this is a short circuit, we don't look for other values |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment