Skip to content

Instantly share code, notes, and snippets.

@swannodette
Created June 9, 2009 21:24
Show Gist options
  • Save swannodette/126802 to your computer and use it in GitHub Desktop.
Save swannodette/126802 to your computer and use it in GitHub Desktop.
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