How to modify out-of-scope variables in JavaScript _ javascript tips-js tutorial

Source: Internet
Author: User
Scope is one of the most important concepts of JavaScript. To learn javascript well, you need to thoroughly understand the principle of javascript scope. Next, we will introduce how javascript modifies out-of-scope variables through this article, if you need a friend to study together, 1. today, I am reading the after-school exercises in the JavaScript Learning Guide. Therefore, I will conduct a more in-depth study on the input parameters of the function.

The questions are as follows:

How can a function modify variables out of its scope? Write a function from 1 ~ An array composed of digits 5 as a parameter. After calling this function, the number items in the function are replaced with the corresponding string representation.

Note:

When passing function parameters in JavaScript and passing values (numbers, strings, and boolean values) based on the original values, changes in the function will not affect the actual parameter values. for the parameters passed to the function, the object is a reference, and the modifications to it will be reflected in the main program. <-however, this may happen as follows:

var outer_number = ;var outer_boolean = true;var outer_array = [,,];var outer_object = {test:""};function display(num,bool,arr,obj){console.log("number:"+num+"\nboolean:"+bool+"\narray:"+arr+"\nobject:"+obj.test);}function test(num,bool,arr,obj){display(num,bool,arr,obj);//num=,bool=true,array=[,,],object.test=num = ;bool = false;arr[] = ;obj.test = "";display(num,bool,arr,obj);//num=,bool=false,array=[,,,],object.test=arr = [,,];obj = {test:""};display(num,bool,arr,obj);//num=,bool=false,array=[,,],object.test=}test(outer_number,outer_boolean,outer_array,outer_object);display(outer_number,outer_boolean,outer_array,outer_object);//num = ,bool=true,array=[,,,],object.test= 

In the above Code, we have created four global variables, whose types are numbers, Boolean values, arrays, and objects. Two functions, display and test.

Display is executed four times and the results are as follows:

"Number: 2
Boolean: true
Array: 1, 2, 3
Object: 122 "<-value when the function is passed in

"Number: 0
Boolean: false
Array: 1, 2, 3
Object: 134 "<-execute change

"Number: 0
Boolean: false
Array: 3, 2, 1
Object: 133 "<-re-assign a value

"Number: 2
Boolean: true
Array: 1, 2, 3
Object: 134 "<-after the function is executed

We can see that our re-assignment of arrays and objects is not successful. If we pass the value by reference, we should also re-assignment of global variable arrays and objects.
In fact, the so-called reference-based value assignment in JavaScript is not a reference-based copy in the true sense. To be precise, it should be transmitted by share. it can also be called transfer by object and transfer by object sharing (call by sharing ).

Under the shared transfer condition, the reference we obtain is only a copy of the real parameter reference, the biggest difference between it and the transfer by reference is that the value we assign to the referenced copy does not affect the value of the real parameter, as we did above, the value assignment operation is not feasible.

Of course, from the perspective of object type and basic type, the object is variable while the basic type is immutable (note! String modification is actually a new string returned), so transfer by share is also consistent with transfer by share for the basic type.

Summary:

In JavaScript, the basic types and objects are all transferred by sharing. However, due to the immutability of the basic types of JavaScript, the transfer by sharing is no different from the transfer by value, objects are transmitted by sharing.

Call by sharing: A copy referenced by a real parameter is passed. the value assigned to the referenced copy does not affect the value of the real parameter. However, you can use the referenced copy to modify the reference content. detailed wiki address

The input parameters of the function pair:

1. for basic types, pass by value (or share by share), internal value assignment changes do not affect the main program

2. Object type, transmitted by share, passed as a copy of the reference of the real parameter. The internal value assignment to this reference is invalid, and the modification to the object attribute value assignment is valid.

This is probably the case. I hope to be pointed out if I make any mistakes.

The JavaScript scope is pulled out separately below.

Any programming language has the concept of scope. Simply put, scope is the accessible scope of variables and functions, that is, scope controls the visibility and lifecycle of variables and functions. In JavaScript, variables have two scopes: global scope and local scope.

   Global Scope)

Objects that can be accessed anywhere in the Code have a global scope. Generally, the following situations have a global scope:

(1) The outermost function and variables defined outside the outermost function have a global scope. For example:

Var authorName = "mountain creek"; function doSomething () {var blogName = "Dream sky"; function innerSay () {alert (blogName);} innerSay ();} alert (authorName); // alert (blogName) of the edge creek; // Script Error doSomething (); // dream sky innerSay () // Script Error

(2) All variables whose values are directly assigned at the end are automatically declared as having a global scope. For example:

Function doSomething () {var authorName = "edge stream"; blogName = "Dream sky"; alert (authorName) ;}dosomething (); // alert (blogName ); // dream sky alert (authorName); // Script Error

The variable blogName has a global scope, and the authorName cannot be accessed outside the function.

(3) All properties of the window object have a global scope

Generally, the built-in properties of the window object have global scopes, such as window. name, window. location, and window. top.

1. Local Scope) 

Unlike global scopes, local scopes are generally accessible only within a fixed code segment. The most common is internal functions, in some cases, we can see that such scopes are called function scopes. For example, the blogName and innerSay functions in the following code only have partial scopes.

Function doSomething () {var blogName = "Dream sky"; function innerSay () {alert (blogName) ;}innersay () ;}alert (blogName ); // Script Error innerSay (); // Script Error

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.