365bet扑克客户端-beat365进不去-365手机app

如何手动销毁javascript中的对象?

如何手动销毁javascript中的对象?

您可以将变量作为键传递到对象中,并将该对象传递给驱逐舰函数。这样,您的函数就可以使用对象的第一个键检索全局变量的名称,然后在窗口对象中删除它。

请注意,这将只适用于全局变量。

​

代码语言:javascript复制// retrieves the first key of the object passed as parameter

function getFirstKeyName(someObject){

return Object.keys(someObject)[0];

}

// destroy a global variable by its name

function destroyGlobalVariable(withinAnObject){

// retrieve the variable's name

var globalVarName = getFirstKeyName(withinAnObject);

console.log("Deleting global variable " + globalVarName);

// use delete, assign undefined or null to window[globalVarName];

delete window[globalVarName];

}

abcObj = {

a:1,

b:2

}

// abcObj should be defined :

console.log(abcObj);

// pass your variable name into an object, as a key

destroyGlobalVariable({abcObj});

// abcObj should not be defined now :

console.log(abcObj);​

相关推荐