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])
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.
You can delete properties on objects, you can't delete variables.
Either assign undefined or let the variable fall out of scope.
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]);
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
Related
Using Chrome I passed in console this name of global variable:
multiConfig
And get result:
multi: {type: "1", containerId: "mp", go: {…}}
__proto__: Object
I try to delete this variable by click:
if (window['multiConfig']) {
delete window['multiConfig'];
}
And I get this error:
ERROR TypeError: Cannot delete property 'multiConfig' of [object Window]
Why?
You cannot delete a window variable, but you can unset it:
window['multiConfig'] = undefined;
Reason:
The window object is not configurable.
You can refer to this - How to unset a JavaScript variable?
Variables declared with var are added as properties on the global window object and cannot be deleted with the delete operator.
From MDN - var:
In the global context, a variable declared using var is added as a
non-configurable property of the global object. This means its
property descriptor cannot be changed and it cannot be deleted using
delete.
Reason for this is also explained:
The property created on the global object for global variables, is set
to be non-configurable because the identifier is to be treated as a
variable, rather than a straightforward property of the global object.
JavaScript has automatic memory management, and it would make no sense
to be able to use the delete operator on a global variable.
Also note that trying to delete a property set on of global window object for variables declared with var fails silently in non-strict mode and throws a TypeError in strict-mode.
Also note that you can delete a property from the window object if is set explicitly.
var a = 1; // can't be deleted
window.b = 2; // can be deleted
delete window.a;
delete window.b;
console.log(window.a);
console.log(window.b);
I read a book about javascript that said:
var o = {x:1 , y:2 };
delete o ; // Can't delete a declared variable so returns false;
However, the book also states that the variables declared outside any function scope are properties of the global object.
Why aren't we allowed to delete it then if it is a property of the global object?
By saying:
var o = {x:1 , y:2 };
in the top level scope, you are declaring a global variable, which can not be deleted. It does create a property on the global object (which is aliased to the window object in browsers), but it is a special property indeed. However, if you make the declaration like:
o = {x:1 , y:2 };
then you are setting a property on the global scope (remember, the window object) implicitly. The two are similar, but different enough. The delete operator removes an implicit property from an object, but will not delete a variable created on the global object.
Edit, found a more thorough answer
https://stackoverflow.com/a/4862268/1443478
Since O is already declared and have properties you cannot use delete on the object. You can use
var o = {x:1 , y:2 };
delete o.x ;
and delete properties here is a DEMO for the same.
first you have to know what is the work of delete operator.let me explain---
"The delete operator removes a property from an object".here i say it removes an object property not a variable.
in your code you declare a variable.not an object so delete does not work.i think you understand.
o = {x:1 , y:2 };
delete o ;
but the code above is right i think. why?
because here o is a property of the global object which is also an object so it work properly.the link which can help you is delete operator
it is my first answer.be happy with coding.
How come a properly declared global variable can't be deleted?
I don't know if this is across all program languages, but I know that in JavaScript it can't be deleted.
Source: Javascript the Definitive Guide O'Reilly.
When you use global variables and you want to be able to delete them, you should easily define them in a global object, without using var in your statement, like:
let's say you want to define a global varible in your code, and you need to be able to delete them whenever you want, so if you do:
function myfunc(){
var name = "Robert";
console.log(delete name);
}
and call it in your console you would have, false as the result of delete statement, which means it has not got deleted, but if you do it like:
function myfunc(){
var obj = {};
obj.name = "Robert";
console.log(delete obj.name);
}
then your result would be true, which means it gets deleted now.
now for global object if you create it like:
window.myobj = {};
then you can delete it and it actually get deleted:
delete window.myobj;
or
delete window["myobj"];
The thing is when you create your variable using var, in the window context, although it is on object in the window, but it doesn't get deleted, for instance if you do:
var myobj = {};
in the browser dev console, it gets defined in the window, and you can have it like:
window.myobj
but you can not delete it, because you have defined it in a var statement.
But do not forget to set it to null, if you really want it to get deleted from memory:
window["myobj"] = null;
delete window["myobj"];
As was stated in this answer by user Eric Leschinski
Delete a variable in JavaScript:
Summary:
The reason you are having trouble deleting your variable in JavaScript
is because JavaScript won't let you. You can't delete anything created
by the var command unless we pull a rabbit out our bag of tricks.
The delete command is only for object's properties which were not
created with var.
JavaScript will let you delete a variable created with var under the
following conditions:
You are using a javascript interpreter or commandline.
You are using eval and you create and delete your var inside there.
or you can set null to an variable which will behave like a deleted object
When variable is created in global scope then automatically DontDelete property is added to the variable and set to the true. That is the reason global variables (or functions too) can not be deleted.
For other variables that property is false so those can be deleted.
For more clarity you can refer the article : understanding delete
With ECMAscript 5, the properties added to an object now have attributes which allow you more control over the object. These attributes are:
value - The actual value of the property
writable - If the property can/cannot be changed.
configurable - If set to false,any attempts to change its attributes will fail in strict mode (and will return false in non-strict mode)
enumerable - if the property can be iterated over when the user does for (var prop in obj) {}
These attributes can be checked with another API exposed by Ecmascript 5 called:
Object.getOwnPropertyDescriptor(obj, prop)
Now, when you create a global variable WITHOUT the 'var' keyword, like so:
sum = function (a, b) { return a + b; }
then this property 'sum' get created on the window object with configurable attribute set to true.
console.log(Object.getOwnPropertyDescriptor(window, "sum"))
... and therefore this property CAN be deleted from the window object.
delete window.sum //returns true
But when you create a property with the var keyword, then configurable property is set to false like so:
var multiply = function (a, b) { return a * b; }
console.log(Object.getOwnPropertyDescriptor(window, "multiply"))
... and now, this property CANNOT be deleted.
delete window.multiply //returns false in non-strict mode
Courtesy: John Resig
function exampleFunction(){
var theVariable = "Lol!";
var variable2 = Lol.toLowerCase();
console.log(theVariable);
delete theVariable; //to prevent bugs, I want to ensure that this variable is never used from this point onward.
console.log(theVariable); //This still prints "Lol!", even though I just tried to delete the variable.
}
In JavaScript, is it possible to prevent a variable from being used in a function after a certain point? I've tried declaring a string called theVariable, and then I tried to delete the variable using delete theVariable, but console.log(theVariable) still prints theVariable's value even after that point.
I tried using delete theVariable to make theVariable unusable from that point onward (in order to prevent myself from accidentally using the variable when it is no longer needed), but it doesn't appear to have that effect. Is there any way to work around this limitation?
One way is to limit its scope. Since JavaScript doesn't have block scope, that requires an IIFE (or similar technique):
function exampleFunction(){
var variable2;
(function() {
var theVariable = "Lol!";
variable2 = Lol.toLowerCase();
console.log(theVariable);
})();
// theVariable is now out of scope, and cannot be referenced
}
In that case you can set the value to undefined like theVariable = undefined
The delete function does not work as you expected
From docs
The delete operator removes a property from an object.
In this case theVariable is not a property of an object, it is a variable in the current function scope.
You cannot delete primitive types, only objects. If you don't want to use a variable after a certain point, just review your code so that you don't use it. Unfortunately, JS does not have block scoping to restrict the visibility of variables. You'll have to check for this manually.
Alternatively, set the value to undefined.
I have a really simple snippet of code and a really (probably) simple change I need to make.
I can't access a variable that I need to in my jQuery script:
var Objects; // Used to store stuff
enable_reordering();
function enable_reordering()
{
$('a.move-object').click(function(){
Objects.moveMe = $(this);
$('#image-title').text( $(Objects.moveMe).attr('data-child-title') );
return false;
});
}
When I try to change the value of Objects.moveMe to anything, my browser moans that Objects is not set. (Error: Objects is undefined).
How can I make it so that I can use variables in and out of functions throughout my entire script?
Update:
The error is caused by the line
$('#image-title').text( $(Objects.moveMe).attr('data-child-title') );
where I first try and use the variable.
try: http://jsbin.com/ocodoz/
var a;
alert(a);
a === undefined But declared in the current scope..
Your Object have to be set to an object
var Objects = {};
It's not a scope issue. The problem is that, as the error says, Objects is undefined. It looks like you want to set a property of it, so initialize it as an object literal:
var Objects = {};
Currently, what you are trying to do is effectively:
undefined.moveMe = $(this);
When you declare a variable, its value is undefined until you assign some other value to it. By assigning an empty object literal to it, you can then set properties of that object.