javascript - delete operator with var and non var variables - Stack Overflow

I tried delete a variable in javascript using delete operator, but found some issue.Can you guys pleas

I tried delete a variable in javascript using delete operator, but found some issue. Can you guys please explain the below code, and why it is happening

>> var a = 5;

>> delete a
false

>>a
5

>> b=5;

>>delete b
true

>>b
ReferenceError b is not defined

why var a = 5 and b = 5 are different?

I tried delete a variable in javascript using delete operator, but found some issue. Can you guys please explain the below code, and why it is happening

>> var a = 5;

>> delete a
false

>>a
5

>> b=5;

>>delete b
true

>>b
ReferenceError b is not defined

why var a = 5 and b = 5 are different?

Share Improve this question edited Mar 11, 2014 at 6:03 Rahul Tripathi 173k33 gold badges291 silver badges339 bronze badges asked Mar 11, 2014 at 5:46 jforjsjforjs 4731 gold badge5 silver badges12 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

When a variable is created using a variable declaration (i.e. using var) then it is created with its deleteable flag set to false.

When a variable is created implicitly by assignment without being declared, its deleteable flag is set to true.

It is a peculiarity of the global execution context that variables are also made properties of the global object (this doesn't happen in function or eval code). So when you do:

var a;

Then a is a variable and also a property of the global (window in a browser) object and has its deleteable flag set to false. But:

a = 'foo';

creates a as a global variable without a declaration, so its deleteable flag is set to true.

The result is that you can delete global variables created implicitly, but not those created by declarations (including function declarations).

Without using var, assignment with the = operator is always assigning a property, in your second case the object is implicitly the global object (window in your browser).

The delete operator is only for deleting properties on objects, not normal variables.

The delete operator deletes an object, an object's property, or an element from an array. The operator can also delete variables which are not declared with the var statement.

delete objectName.property 
delete objectName[index] 

The delete operator removes a property from an object not the normal variables. Here in this case the object is global implicitly.

On a side note:-

When you write var x = 5 then it declares variable x in current scope ie, in execution context. If declaration appears in a function then local variable is declared and if it's in global scope then global variable is declared.

Whereas when you say x = 5,then it is merely a property assignment. It first tries to resolve x against scope chain. If it finds x anywhere in that scope chain then it performs assignment otherwise it creates x property on a global object.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744276434a4566351.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信