There's a difference between Var and no Var in javascript.

Source: Internet
Author: User

var web = $ ("#qianduan");

Web.click (function () {

Alert ("hahaha");

});

When we create a variable, we usually write it like this:

var abc = 123;

At the same time, we can create variables when we do not write Var, which is written in the JS high-level program:

The global environment in JS is an object, this object is also the root of the JS run, the browser of JS, this object is the Window object. For a global JS statement, the Window object is equivalent to the current scope.

When we write down:

varMyJs=‘Start‘;

is a variable myjs that defines the scope of the window, when we write:

MyJs=‘Start‘;

is to define a property myjs for the window scope.

From the above, the window scope of a variable myjs and the window scope of a property myjs almost equivalent, for the global JS statement to add var and no difference, but in the function body of the statement Plus and do not add a difference.

<script language="JavaScript"Type="Text/javascript">varMylike ='C #'; Youlike='C + +'; Alert ('I like:'+ Mylike +"you like:"+youlike); //output: I like: C # you like: C + +changelike (); function Changelike () {alert (Mylike+'Yes, I like it .'+ Youlike +'you like it .');//undefined yes I like C + + Yes you like        varMylike ='JS'; Youlike='JAVA'; Alert (Mylike+'Yes, I like it .'+ Youlike +'you like it .')//JS is I like Java is you like} alert (Mylike+'Yes, I like it .'+ Youlike +'you like it .')//C # Yes I like Java yes you like</script>
from the above we can see that there is a var modifier in the function inside and outside the display of two things, no Var modified only show a thing, its value function can be changed inside and outside. There's a undefined on it. Everyone should find it strange, why not C #. As mentioned above, in the JS global statement there is Var is a variable, nothing is a property. Variables are scoped, and the code goes in the following function, which is equivalent to creating a sub-scope in the current scope. The JS engine will first create the custom function as a variable function on the scope, the variables defined for all VAR, pre-compile will be defined as UNDEFINED,JS encountered variables and functions will be first found in the current scope, did not find the upper layer to find, and var mylike = ' JS '; The description already exists, and the initial value is defined as Undefined,var mylike = ' JS ' assignment at the back of the output, so it is undefined.

I listened to the radio last night and talked about the way it didn't add Var.

It creates a property under window, and it can be deleted, and the VAR declared variable can only be reclaimed by the garbage collection mechanism and cannot be deleted, which is the difference between it and VAR declaration variable.

Such a listen, feel very reasonable, so immediately to try the delete.

var ccc=22;

Delete CCC;

Console.log (CCC);

Then try not to add Var

SSS = 33;

Console.log (SSS);

Delete sss;

Console.log ("after delete" +sss);

There's a difference between Var and no Var in javascript.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.