最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to delete object from window javascript - Stack Overflow

programmeradmin3浏览0评论

I need to delete one object in my case. so i am using "delete" keyword but after using it, I am able get the value again

var test= {};
test[0]="111";
test[1]="555";
delete test;
alert(test[0])

I need to delete one object in my case. so i am using "delete" keyword but after using it, I am able get the value again

var test= {};
test[0]="111";
test[1]="555";
delete test;
alert(test[0])
Share Improve this question asked Feb 5, 2014 at 11:54 JitenderJitender 7,97132 gold badges115 silver badges218 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 7

You can't delete a local variable that has been declared with var.

You can only delete properties of objects - this happens to also include global variables which are implicit properties of the window object.

As has been mentioned, you can't delete a variable that has been declared with var.

For example, if you were to change your code to the following - so that test is an explicit property of window - the delete will work.

window.test = [];
window.test[0]="111";
window.test[1]="555";
delete window.test;
alert(window.test[0]);

You can delete properties on objects, you can't delete variables.

Either assign undefined or let the variable fall out of scope.

Whenever delete, it returns a boolean that tells wether it could delete the var or not. In this case, it returns false:

delete test;    // false

You can just set test to undefined:

test = undefined;

you can use test = undefined to make remove object value

发布评论

评论列表(0)

  1. 暂无评论