JS Getting Started tutorial [first season of JavaScript learning]

Source: Internet
Author: User
Tags getdate
JavaScript Learning first season (2)
The previous article talked about some concepts (lexical structure) and data type (part) in JS.
Let's continue with this chapter. Then understand the scope of operation data and functions in the next JS.
1, the conversion between the object and the basic type:
The object is not empty at any time, and is true in a Boolean environment.
As
New Boolean ( false );
New number (0);
New String ( "" );
New Array ();
Although the internal value is false, the value of the object is true;
Object à valueof () à toString ()
Where the date class is to perform the ToString () conversion first.
To manipulate a data value in 2,JS:
Any language has its own method of manipulating data;
J S is no exception, JS has 3 important ways to manipulate a data value.
1) copy it. For example, assign it to a new variable.
2) Pass it as a parameter to a function or method.
3) can be compared to other values of size.
J s through pass value and pass the address 2 ways to manipulate the value of this data.
from the name can look at the place, Pass Value is to manipulate the data by passing values. In the process of assignment, the actual value is copied and stored in a new variable. The value of the copy and the original value are 2 completely independent values. So if you change the value of the copy, it doesn't affect the original value. When you compare the size, you usually do a byte comparison.
Address from the name point of view, it is by passing the address to manipulate the data. In the process of assigning values, the address of the actual value (which can be referred to as a reference) is copied, and they are not completely independent, so if you change the value by reference, the original value will change. When comparing sizes, it is usually to see if they refer to the same address for comparison.
Simple example of an address:
var a = new Date ();
Alert (A.getdate ());
var B = A;
B.setdate (21);
alert (A.getdate ()) //Output
3, in general:
The base data type is manipulated by passing values. (If you forget what the basic data type is, you can look back.) )
The object data type is manipulated by an address. (such as arrays and functions)
Example:
<script>
Pass Value
A=1;
B=a;
b=2;
alert (a); //Output 1
Address
x=[1,2];
y=x; //To Y is just a reference to X, not x itself. The array has been assigned to the statement, and after executing this code, there is still only one array object, but we have 2 references to him.
y[0]=2;
alert (x[0] + "| "+x[1]);   //Output 2 | 2
</script>
Where we have to pay attention to strings:
in JS, strings are copied and passed through the address, and they are passed Pass Value to compare.
objects and arrays are passed by value, except that the value passed is actually a Reference , rather than object itself .
Summarize:
Type
Copy
Passed
Comparison
Digital
Pass Value
Pass Value
Pass Value
Boolean
Pass Value
Pass Value
Pass Value
String
Not to change
Not to change
Pass Value
Object
Address
Address
Address
Immutable: In JS, there is no way to change the contents of a string value.
For a string, it doesn't make much sense to pass a value or to pass the address.

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.