Created
October 22, 2010 15:31
-
-
Save andrew8088/640764 to your computer and use it in GitHub Desktop.
Playing around with FireFox's __noSuchMethod__ property; all browsers should have this!
This file contains 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
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