Note the following before getting started with javascript: Variable Section

Source: Internet
Author: User
In the HTML5 era, the requirements for JavaScript are not reduced, but improved. The javascript language is very easy to get started with. If you have the basics of C-style structured languages such as Java and C #, you can write JavaScript for at most half a day. However, JavaScript is a dynamic language. This feature makes it very different from Java, C #, and other languages in many aspects. Many people say that javascript can be implemented in a short time as long as Java and C # are used. It is estimated that javascript can be used to write simple applications, if you want to have a good grasp of JavaScript, You need to really understand the dynamic feature of JavaScript.
After you quickly get started with JavaScript programming, before improving development capabilities, you need to be sure to master the programming details or special features of JavaScript.
In addition, JavaScript has the same compatibility issue with CSS in different browsers, and has different changes in different browsers and versions.

I think there are two problems that affect developers after the entry of JavaScript and before the development to intermediate (Object-Oriented Programming): Type dynamics and syntax flexibility. The collection of these two points is the core of a variety of strange problems caused by many entry-level JavaScript workers. In this article, I will not discuss the basics of syntax, the methods of core objects, and Dom processing. These contents are available in any JavaScript tutorial on the Internet, we will discuss some code-level-based but error-prone points to lay a good foundation for your future high-quality JavaScript.

The following content is tested in IE7/8, chrome14, and firefox10. At the same time, when declaring a thing, the following content is more casual and more casual than JavaScript, not as a step-by-step tutorial. You need at least some JavaScript code experience.

Variable Section

In JavaScript, variables declared using VAR are the variables in the current scope, and those declared without VaR are definitely global variables.

<SCRIPT type = "text/JavaScript"> var x = 10; // global Y = 100; // global function fun1 () {var M = 99; // local n = 9; // global} alert (typeof this. x); // number alert (typeof this. y); // number alert (typeof this. m); // undefined alert (typeof this. n); // undefined fun1 (); alert (typeof this. m); // undefined alert (typeof this. n); // number </SCRIPT>

The code above indicates that after fun1 is executed, the N variable declared by VAR is not used in the function and is registered as a global variable.

The following code achieves the same execution effect.

<SCRIPT type = "text/JavaScript"> var x = 10; // global Y = 100; // global function fun1 () {var M = 99; // local n = 9; // global} alert (typeof window. x); // number alert (typeof window. y); // number alert (typeof window. m); // undefined alert (typeof window. n); // undefined fun1 (); alert (typeof window. m); // undefined alert (typeof window. n); // number </SCRIPT>

Then, we can obviously get a inference. In the global context, window and this are the same object pointing.

<script type="text/javascript">    alert(this == window);    alert(this === window);</script>

In this case, the following functions have different directions for this between the function state and the object state, because the context is calculated only when Javascript is executed dynamically.

<script type="text/javascript">    function Fo1() {        return this;    }    alert(Fo1());    alert(new Fo1());    </script>

As a variable, variables declared using VAR cannot be deleted. variables declared without VaR can be deleted.

VaR x = 10; // global Y = 100; // global function fun1 () {var M = 99; // local n = 9; // global Delete m; delete N; alert (typeof m); // number alert (typeof N); // undefined} Delete X; Delete y; alert (typeof X ); // numberalert (typeof y); // undefinedfun1 (); alert (typeof N); // undefined

Declaring variables in Javascript is very flexible, but this flexibility requires some attention to global and local control.

Function fun1 () {var M = n = 10; // n indicates global, M indicates local var x, y = 1, K = 10; // xyk indicates local}

What's interesting is that in common languages, variables are always defined and used first, but in Javascript ...... See the following code

function fun1() {    alert(typeof x); //undefined    alert(typeof y); //undefined    var x = 10;    alert(typeof x); //number}fun1();

This result is completely acceptable and expected to you, ......

Function fun1 () {alert (x); // undefined alert (y); // The system prompts that y does not define var x = 10; alert (X ); // number} fun1 ();

The above phenomenon is very strange. If we really want to use X and Y, the compiler will treat them differently and think that X is just not defined (approval has been declared ), Y does not exist. This means that when dealing with a range variable, VaR is always allocated at the beginning no matter where you declare it. For variables not defined by VAR, there will be no such treatment, and they must be allocated only after they are executed.

Function fun1 () {alert (x); // prompt that X does not define alert (y); // prompt that y does not define x = 10; alert (X ); // number} fun1 ();


Note that the VaR variable declared in the function is not limited to the declared code statement block. Take a look at the following code:

function fun1() {    for (var i = 0; i < 10; i++) {    }    alert(i); //10}fun1();

Therefore, we recommend that you declare all the local variables in the first line of an object/function.

function fun1() {    var value = 1,    arr = [],     obj = {},    date = new Date(),    has = false;}

Note that variables are separated by commas (,). The advantage is obvious. Consider it yourself.
The syntax of JavaScript is flexible, and the statement can be left behind; to indicate the end. At this time, the compiler will say that the hard carriage return is used as the end of the statement.

function fun1() {    var x = 10    var y = 9    return x + y}alert(fun1()); //19

I don't know any special advantages in writing the code above. If you want to show off the code, you don't need to use it. Let's just end with a concluding sentence. Because the following code will get an error

function fun1() {    return     {        Title: "title",        Style: "style",        Value: "Value"    }}alert(typeof fun1()); 

Because JavaScript will add one after return;

The correct method is

function fun1() {    return {        Title: "title",        Style: "style",        Value: "Value"    };}alert(typeof fun1()); 

I cannot say that this must be caused by not writing, but I want to explain that you should be careful with JavaScript's arbitrary code processing: you should always let the Javascript compiler know exactly what you want, instead of making guesses.

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.