Skip to content

Instantly share code, notes, and snippets.

@andrew8088
Created October 22, 2010 15:31
Show Gist options
  • Save andrew8088/640764 to your computer and use it in GitHub Desktop.
Save andrew8088/640764 to your computer and use it in GitHub Desktop.
Playing around with FireFox's __noSuchMethod__ property; all browsers should have this!
var myObj = (function () {
var data = {
products : ["foobar", "bazquirk", "gizmo"],
services : ["widgets", "other", "last"]
};
return {
__noSuchMethod__ : function (id, args) {
if (id === 'all_products') return data.products;
if (id === 'all_services') return data.services;
if (id.indexOf('_that_start_with_') > -1) {
var ids = id.split("_that_start_with_"),
arr = [], item, i = 0;
if (data[ids[0]]) {
for ( ; item = data[ids[0]][i]; i++ ) {
if (item.indexOf(ids[1]) === 0) {
arr.push(item);
}
}
return arr;
} else {
return [];
}
}
}
};
})();
console.log(myObj.all_products());
console.log(myObj.all_services());
console.log(myObj.products_that_start_with_f());
console.log(myObj.services_that_start_with_o());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment