JavaScript Advanced Programming (2nd edition) Note 2

Source: Internet
Author: User

Note 1 Extracts some of the information about JavaScript, its composition, its basic types, and other irrelevant essentials, but the individual still feels that these basics must be understood, after all, better than nothing.

And today, there are some questions that sometimes make beginners wonder, such as JavaScript variables, scopes, and memory, especially the scope (C #, Java and other high-level language developers you know!) , do not understand when the head big is inevitable drop!

However, as a developer (program Ape), when encountering these big head problems, can only explain that they are not "proficient".

(iv) variables, scopes, and memory issues

1 scope (global scope)

Objects that can be accessed anywhere in the code have global scope, typically with global scope in the following situations:

(1) The outermost function and the variables defined outside the outermost function have global scope, for example:

var Name= "Zhang San"; function dosomething () {    var newname= "John Doe";     function Innersay () {        alert (newName);    }     // Zhang San // Script Error // John Doe // Script Error 

(2) All variables that do not have a direct assignment defined are automatically declared as having global scope

function dosomething () {
var Name= "Zhang San"; NewName= "John Doe"; // Zhang San // John Doe // Script Error

The variable blogname has a global scope, and AuthorName cannot be accessed outside the function.

(3) All Window object properties have global scope

In general, the built-in properties of the Window object have global scope, such as Window.name, Window.location, Window.top, and so on.

1 scope (local scope)

In contrast to the global scope, local scopes are generally accessible only within a fixed code fragment, the most common of which is inside the function, and in some places it is also seen that this scope is called a function scope, such as the Blogname in the following code and the function Innersay only have local scope.

function dosomething () {    var newname= "John Doe";     function Innersay () {        alert (newName);    }     // Script Error // Script Error

Today is mainly the scope of some understanding, may be a bit messy ...

  

JavaScript Advanced Programming (2nd edition) Note 2

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.