forked from locutusjs/locutus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunset.js
More file actions
24 lines (23 loc) · 1003 Bytes
/
Copy pathunset.js
File metadata and controls
24 lines (23 loc) · 1003 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function unset () {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brett-zamir.me)
// * example 1: var arr = ['a', 'b', 'c'];
// * example 1: unset('arr[1]');
// * returns 1: undefined
// Must pass in a STRING to indicate the variable, not the variable itself (whether or not that evaluates to a string)
// Works only on globals
var i=0, arg = '', win='', winRef=/^(?:this)?window[.[]/, arr=[], accessor='', bracket=/\[['"]?(\d+)['"]?\]$/;
for (i=0; i < arguments.length; i++) {
arg = arguments[i];
winRef.lastIndex = 0, bracket.lastIndex = 0;
win = winRef.test(arg) ? '' : 'this.window.';
if (bracket.test(arg)) {
accessor = arg.match(bracket)[1];
arr = eval(win+arg.replace(bracket, ''));
arr.splice(accessor, 1); // We remove from the array entirely, rather than leaving a gap
}
else {
eval('delete '+win+arg);
}
}
}