javascript: Declaration of variables
Here are a few ways to declare variables
Copy Code code as follows:
var value;
var value,value1,value2;//declares multiple variables at the same time, but the values of these variables are all undefined
var i = 0,j = 0,k=100;//variable declaration, initialized in one.
If you try to read a nonexistent variable (value) will be an error! But trying to assign a value to a variable that is not declared with Var, javascript
The amount of change is implicitly declared, and the declared variable is global. Details: So you create variables all try to use Var
The scope of the variable (this problem is also easy to get out, we need to understand)
javascript: Scope of variables
These are the details, and I like the beginner must pay attention to avoid!
Copy Code code as follows:
var Golbal = "Golbal"; Global variables
var local = ' local ';
function Area ()
{
Local variables have higher precedence than global variables
var local = "Arealocal"
JavaScript hides global variables when the variable name declared in the function body is the same as the global variable name
var Golbal = "Areagolbal";
document.write ("Local is:" +local + "and Golbal is:" + Golbal + "<br/>");
}
Area ();
Output: local Is:arealocaland golbal Is:areagolbal
What happens when you define a local variable inside a nested function? Look below:
Copy Code code as follows:
var hope = "Moremoney";
function Createmore ()
{
var hope = "have more money";/local
function Createmoreto ()//nested functions
{
var hope = "have more";/local
document.write ("Createmoreto hope is:" +hope + "<br/>");
Output: Createmoreto Hope Is:have
}
Createmoreto ()//Call
document.write ("Createmore hope is:" +hope + "<br/>");
Output: Createmore Hope Is:have
}
Createmore (); Call
javascript: Passing values and passing addresses
This is also a more important concept! Don't miss it.
|
Pass Value |
Address |
Copy |
The actual copied value, the existence of a different, independent copy. |
Only a reference to a number is copied. If the value is modified by this new reference, the change is also visible to the original reference. |
Passed |
The independent copy of the value passed to the function has no effect on its change outside the function |
A reference to a value is passed to a function, and the change is visible if the function modifies the value by passing the reference to it. |
Comparison |
Compares these two opposing values, usually byte by comparison, to determine whether equality |
Two references are compared to determine whether they are referring to the same numeric value. |
javascript: base type and reference type
The basic rule of JavaScript is that the base type operates by passing values, and the reference type is manipulated by the address. (What's the value type, or what's the reference to see my last article)
Pass by value
Copy Code code as follows:
var value = 1;
var copyvalue = value; Assign value to another variable
function AddTotal (TOTAL,ARG)
{
total+= Arg; Total = total + arg effect equals
}
Call the function, pass two arguments (you may think that this function changes the value of the global variable, in fact, the function is also the opposite copy)
AddTotal (Value,copyvalue);
if (value = = 1) copyvalue = 2;
document.write ("total \ t" + Value + "and Copyvalue \t\t" + copyvalue+ "<br/>");
Final output: Total 1and copyvalue 2
delivered by address
Copy Code code as follows:
var array = new Array ("JAVASCCCP");
var objarray = array;
function Modifyarray (arr)
{
Arr[0] = "JAVASCRIPT";
}
Before calling a function
document.write (Array[0] + "<br/>");
Output JAVASCCCP;
After calling the function
Modifyarray (array);
document.write (array[0]+ "<br/>");
Output uppercase JavaScript
By modifying the Objarray will be the same effect
Objarray[0] = "Frank";
document.write (array[0]+ "<br/>");
Output Frank;
Summary: Above content hope everybody don't miss, to study behind of knowledge still very helpful!