JavaScript data structure and algorithms chapter I (programming experience) three __ios

Source: Internet
Author: User
Tags data structures function definition variable scope

Variable scope


A variable scope is a program in which the value of a variable can be obtained. A JavaScript function scope is defined as a function scope, which means that the value of a variable is visible in the function that defines and declares the variable, including any nested functions in the function.

When a variable is defined outside the function, the variable will have a global scope in the main program. This means that its value can be obtained from anywhere in the program, including functions.

The following applet demonstrates how global variables work.

function Showscope () {

return scope;

}

var scope = "global";

print (scope); Print "Global"

Print (Showscope ()); Print "Global"


function Showscope () can get to scope of a variable because scope is a global variable.

Global variables can be defined anywhere in the program. Either before or after the function definition.

Now, let's take a look at what happens when we define another scope variable in the scope of the function.

function Showscope () {

var scope = "local";

return scope;

}

var scope = "global";

print (scope); Print "Global"

Print (Showscope ()); Print "Local"


The scope of a variable defined in the Showscope function has a local scope. The scope defined in the main program has a global scope. Even though their names are the same, their scopes are different, and their values are not the same when they are defined in the region.

All of these actions are normal and expected. However, if you omit the keyword var when the variable is defined, all of this will change.

JavaScript allows you to define variables without using the keyword VAR, and if you do, the variable will automatically have a global scope, even if it is defined in a function.

Example 1-11 demonstrates the consequences of not using keyword var to define variables.

Example 1-11 uses the results of a global variable over and over.

function Showscope () {

Scope = "local";

return scope;

}

Scope = "global";

print (scope); Print "Global"

Print (Showscope ()); Print "Local"

print (scope); Print "Local"

In example 1-11, because the scope variable in a function does not use the keyword Var, when the string "local" is assigned to the variable, we actually change the scope of the variable in the main program. You should use the VAR keyword to avoid such occurrences every time you define variables.

In the past, we mentioned that JavaScript has a function scope. This means that JavaScript has no block scope, unlike other modern programming languages, in the case of a block scope, you can declare a variable in a block, and the variable will not be accessed outside the block. Typical C + + Or a Java for loop.

for (int i=1; i<=10;++1) {

count<< "Hello World" <<endl;

}

Even if JavaScript doesn't have a block scope, when we write the For loop, we assume it does.

for (Var i=1 i <=10; ++i) {

Print ("Hello World");

}

We don't want you to get caught up in the bad habit of writing junk code.

Recursion


In JavaScript, a function call can be called recursively. The function factorial () function defined previously can be written in recursive form.

function factorial (number) {

if (number = = 1) {

return number;

}else {

return number * factorial (number-1);

}

}

Print (factorial (5));


When a function is called recursively, when recursion is continuing, the result of the calculation is temporarily suspended (yield). To show how it works, here's a diagram showing how to work when passed to factorial () parameter 5

5 * Factorial (4)

5 * 4 * factorial (3)

5 * 4 * 3 * factorial (2)

5 * 4 * 3 * 2 * factorial (1)

5 * 4 * 3 * 2 * 1

5 * 4 * 3 * 2

5 * 4 * 6

5 * 24

120

Several of the algorithms we discuss in this book use recursion, and in most cases JavaScript can solve fairly deep recursive calls. (The above is a relatively shallow recursive call.), but in some cases, the algorithm needs to be more recursive so that JavaScript cannot be solved, we can try the iterative algorithm.

You should always remember that any function that uses recursion can be rewritten iteratively.

Objects and object-based programming


The data structures discussed in this book are implemented through objects. JavaScript provides a number of ways to create and use objects. In this module, we demonstrate the techniques used in this book to create objects and use object functions, attributes.

Object defines a constructor that declares object properties and methods, followed by the definition of a function. The following is a constructor that verifies the account object.

function Checking (amount) {

This.balance = amount; Property

This.deposit = deposit; function

This.withdraw = withdraw; function

this.tostring = toString; function

}

The This keyword is used to connect each method and property to an object instance, and now let's look at the definition of the function.

We need to use the This keyword again to modify the balance property so that the interpreter knows which object's balance attribute we are referencing.

Example 1-12 provides a complete definition of an account-checking object, including a test program.

function Checking (amount) {

This.balance = amount;

This.deposit = deposit;

This.withdraw = withdraw;

this.tostring = toString;

}

Function Deposit (amount) {

This.balance + = amount;

}

function withdraw (amount) {

if (amount <= this.balance) {

This.balance-= amount;

}

if (Amount > This.balance) {

Print ("insufficient funds");

}

}

function toString () {

Return "Balance:" + this.balance;

}

var account = new Checking (500);

Account.deposit (1000);

Print (account.tostring ()); balance:1500

Account.withdraw (750);

Print (account.tostring ()); balance:750

Account.withdraw (800); Displays "Insufficient funds"

Print (account.tostring ()); balance:750

Summarize


In this chapter, we preview the way we use JavaScript throughout the book.

We try to follow the programmer's familiar C-style (C + + and Java) programming style. Of course, JavaScript has a lot of conventions that don't follow those languages. We take it for granted and demonstrate the correct way to use the language. We also follow as many good JavaScript programming experiences as the other authors point out. As a responsible programmer, We must always remember that our code can be executed by machines and understood by other programmers!


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.