There is a method in PHP called extract which does exactly what I want to do here. Say I have an object that looks like this:
var data = {
name: "Olly"
age: 19
};
I want to run a method like extract(data) so that I can then access the properties in that object by just using name and age, instead of data.name and data.age.
I’ve done a bit of Googling and couldn’t find anything.
You can use something like this:
(Try that here: http://jsfiddle.net/dHDxd/3/)
Or even export them to global namespace, by using
window[key] = data[key].In any case, be very very careful with that, since the risk of clobbering the global namespace / override other stuff / etc. is very high.
Update: general-purpose
extract()