??????? ????? ???
on 2012-04-17 15:28:17
They have done such a great job with this. This is why they are deserving of these awards. Keep up the good work.
Brett Zamir
on 2008-12-03 17:53:53
Again, adapting the PHP manual's examples, here is an equivalent for extract():
var size = "large";
var var_array = {"color" : "blue",
"size" : "medium",
"shape" : "sphere"};
extract(var_array, 'EXTR_PREFIX_SAME', "wddx");
alert([color, size, shape, wddx_size].join()); // blue,large,sphere,medium
// Only works on global variables
function extract (arr, type, prefix) {
for (var i in arr) {
switch (type) {
case 'EXTR_PREFIX_SAME' || 2:
if (window[i] !== undefined) {
window[prefix+'_'+i] = arr[i];
}
// Fall-through
case 'EXTR_SKIP' || 1:
if (window[i] === undefined) {
window[i] = arr[i];
}
break;
case 'EXTR_PREFIX_ALL' || 3:
window[prefix+'_'+i] = arr[i];
break;
case 'EXTR_PREFIX_INVALID' || 4:
if(i.match(/^[_a-zA-Z$][\w|$]*$/) != null) { // Refine regexp to allow JS 1.5+ Unicode identifiers
window[i] = arr[i];
}
else {
window[prefix+'_'+i] = arr[i];
}
break;
case 'EXTR_IF_EXISTS' || 6:
if (window[i] !== undefined) {
window[i] = arr[i];
}
break;
case 'EXTR_PREFIX_IF_EXISTS' || 5:
if (window[i] !== undefined) {
window[prefix+'_'+i] = arr[i];
}
break;
case 'EXTR_REFS' || 256:
throw 'The EXTR_REFS type will not work in JavaScript';
break;
case 'EXTR_OVERWRITE' || 0:
// Fall-through
default:
window[i] = arr[i];
break;
}
}
}
Karol Kowalski
on 2008-02-06 15:04:39
This version is not recursive as it should be acc to the php manual. Try run following example (taken from the manual, too):
var city = "San Francisco";
var state = "CA";
var event = "SIGGRAPH";
var location_vars = new Array("city", "state");
var result = compact("event", "nothing_here", "location_vars");
console.log(result)
What you get is an object with event property and location_vars property which is an Array. What should be returned is a 'flat' object with event, city and state properties (because location_vars) should be searched inside.