The method of JavaScript modifying extraterritorial variables _javascript tips

Source: Internet
Author: User

1. Today in reading the JavaScript Learning Guide to do after the after-school exercises, but also detailed on the function of the incoming parameters of a more in-depth study.

The topics are as follows:

How can a function modify a variable outside its scope? Write a function that consists of an array of 1~5 numbers as arguments, and the function is called to replace the number items in the corresponding string representation.

Need to pay attention to knowledge points:

The passing of function parameters in JavaScript, for values passing (numbers, strings, Boolean values) based on the parameters of the original value, the modifications in the function do not affect the actual parameter values. The object is a reference to the parameter passed to the function, and its modification will be reflected in the calling program. <-but, There will be such a situation 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);

In the code above we created 4 global variables, the type is numeric, Boolean, Array, object. 2 functions, display and test.

Display performed 4 times, respectively, the results are as follows:

"Number:2
Boolean:true
array:1,2,3
object:122 "<-The value of the passed function

"Number:0
Boolean:false
array:1,2,3,3
Object:134 "<-to perform changes

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

"Number:2
Boolean:true
array:1,2,3,3
object:134 "<-function completed

We can see that we have not succeeded in the redistribution of our arrays and objects, and if we are passing by reference, we should also modify the array and object values of global variables.
In fact, what is called by reference in JavaScript is not really a copy by reference, but it should be delivered by share. It can also be called by object-sharing delivery (call by sharing).

In this condition, which is passed by share, the reference we get can be said to be just a copy of the argument reference, and the biggest difference between what we often say by reference is that our assignment to the reference copy does not affect the value of the argument, as we have done above, the assignment is not feasible.

Of course we look at the object type and the basic type, the object is mutable and the base type is immutable (note! The string modification is actually the new string returned), so sharing by share is also consistent with shared delivery for the base type.

To sum up:

In JavaScript, basic types and objects are shared by share (call by sharing), but because of the invariance of the basic type of JavaScript, the base type is shared and passed by value without any distinction, and objects are passed by shares.

By share delivery (call by sharing): Passing a copy of the argument reference, our assignment to the reference copy does not affect the value of the argument, but you can use the reference copy to modify the referenced content. Detailed Wiki Address

function on parameters passed in:

1. Basic type, by value transfer (or also can be said to share delivery), the internal assignment modification does not affect the keynote program

2. Object type, passed by share, incoming copy of argument reference, internal assignment to the reference is invalid, assignment modification to object property is valid.

Presumably it is so understood, if there is any place I made a mistake, also hope to be pointed out.

Pull out JavaScript scopes separately below

Any programming language has the concept of scope, in short, the scope is the accessibility of variables and functions, that is, the scope controls the visibility and life cycle of variables and functions. In JavaScript, the scope of a variable has two kinds, global scope and local scope.

   global scope (scope)

Objects that can be accessed anywhere in your code have global scope, and typically have global scope in the following scenarios:

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

var authorname= "Mountain Side Creek";
function dosomething () {
var blogname= "Dream Sky";
function Innersay () {
alert (blogname);
}
Innersay ();
}
alert (AuthorName); Mountain Side Brook
Alert (blogname);//Script error
dosomething ();//Dream Sky
Innersay ()///Script error

(2) All variables that are directly assigned at the end of the definition are automatically declared to have global scope, for example:

function dosomething () {
var authorname= "mountain Side Brook";
Blogname= "Dream Sky";
alert (authorname);
}
DoSomething (); Mountain Side Brook
Alert (blogname);//Dream Sky

The variable blogname has a global scope and authorname cannot be accessed outside of the function.

(3) Properties of all window objects have global scope

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

1. Local scope (global scope) 

As opposed to global scopes, a local scope is generally accessible only within a fixed code fragment, most commonly such as within a function, and in some places it is seen as a function scope, for example, Blogname and function Innersay in the following code have only local scopes.

function dosomething () {
var blogname= "Dream Sky";
function Innersay () {
alert (blogname);
}
Innersay ();
}
alert (blogname); 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.