'Ecmascript basics 'in Javascript Advanced Programming'

Source: Internet
Author: User
Tags bitwise operators

1. variables are weak types.
It can store any type of values. For example:
VaR test = "hi ";
Alert (TEST); // output "hi"
Test = 55;
Alert (TEST); // outputs "55"

2. Original Value and reference value.
Variables can be stored in two types of values: the original value and the reference value.
Primitive value is a simple data segment stored in a stack. That is, the location where the variable is accessed.
Reference value is an object stored in heap. The value in the variable (stack space) is the address of the object in the heap.

 

3. original type.
The original types include undefined, null, Boolean, number, and string.
Undefined: A value that declares a variable but is not initialized.

Null: used for objects that do not exist.
Boolean value: There are two values: true and false

Number: both a 32-bit integer and a 64-bit floating point number

String: stores Unicode strings.

 

4. original type conversion.
(1) convert to a string.
Use the tostring () method. Boolean outputs "true" or "false". For the number type, the default mode and base mode are used. For example:
VaR bfound = false;
Alert (bfound. tostring (); // outputs "false"

VaR inum1 = 10.0;
Alert (inum1.tostring (); // outputs "10" // default mode
VaR inum2 = 10;
Alert (inum2.tostring (2); // outputs "1010" // base Mode
Alert (inum2.tostrin (8); // outputs "12"
(2) convert to a number.
Use parseint () and parsefloat (). Only string type conversion is required, and Nan is returned for other types. For example:
VaR inum1 = parseint ("123 blue"); // return 123 ***
VaR inum2 = parseint ("0xa"); // return 10
VaR inum3 = parseint ("22.5"); // return 22
VaR inum4 = parseint ("blue"); // return Nan
(3) force type conversion.
<1> Boolean (value ):
VaR b1 = Boolean (""); // false
VaR b1 = Boolean (0); // false
VaR b1 = Boolean (null); // false
Others return true
<2> Number (value ):
Number ("123 ABCD"); // Nan // the entire value, not part
<3> string (value): The only difference from tostring () is that the forced conversion of null and undefined types to "null" and "undefined", but tostring () does not work.

5. reference type.
Boolean, number, and string.
(1) number class.
Example:
VaR onumberobject = new number (99 );
Alert (onumberobject. tofix (2); // outputs "99.00" // 2 represents two decimal places
(2) string class.
The property has length.
Methods include charat (), charcodeat (), Concat (), +, indexof (), lastindexof (), localecompare (), slice (), substring (), substr (), tolowercase (), touppercase (), etc. Example:

// Find a single character at a specified position
VaR ostringobject = new string ("Hello World ");
Alert (ostringobject. charat (1); // outputs "e"
Alert (ostringobject. charcodeat (1); // outputs "101" // unicode encoding

// Connection
Sresult = ostringobject. Concat (", oh! "); // Equivalent to sresult = ostringobject +", oh! "
Alert (sresult); // outputs "Hello world, oh! "
Alert (ostringobject); // outputs "Hello World"

// Specifies the position of the substring in another string. If no value is found,-1 is returned.
Alert (ostringobject. indexof ("O"); // outputs "4"
Alert (ostringobject. lastindexof ("O"); // outputs "7"

// The second parameter in indexof (,) indicates the number of characters from which the count starts. For example, if you want to find all the positions of a substring:
VaR Pos = ostringobject. indexof ("O ");//###
While (Pos! =-1) // outpus "4" "7"
{
Alert (POS );
Pos = ostringobject. indexof ("O", POS + 1 );
}

// Create a substring
Alert (ostringobject. substring (3); // outputs "lo world"
Alert (ostringobject. Slice (3); // outputs "lo world"
Alert (ostringobject. substring (); // outputs "lo w" // note that the "0" at the 7 position is not displayed
Alert (ostringobject. substr (3, 2); // outputs "Lo" // two characters starting from the third character //###

// The difference between slice () and substring is that when processing a negative parameter, substring treats it as 0, for example:
Alert (ostringobject. substring (-3); // outputs "Hello World"
Alert (ostringobject. substring (3,-4); // outputs "El"
Alert (ostringobject. Slice (3,-4); // outputs "lo w"

6. typeof and instanceof
VaR stemp = "test string ";
Alert (typeof stemp); // outputs "string"

VaR ostringobject = new string ("Hello World ");
Alert (ostringobject instanceof string); // outputs "true" // Q: "Is the variable ostringobject a string-type instance? "

7. Operators
Void returns undefined, which is usually used to avoid output values.
<A href = "javascript: window. Open ('about: blank ')"> click me </a> <! -- Here window. open () returns a newly opened window, which is converted to the string to be displayed. Therefore, an error occurs and should be: -->
<A href = "javascript: void (window. Open ('about: blank ')"> click me </a>

(1) bitwise operators
Not ~, And &, or |, XOR ^
Here, not is processed in three steps: Convert to 32 numbers, reverse the binary number, and convert the reversed code to a floating point number. For example:
VaR inum1 = 25;
VaR inum2 = ~ Inum1;
Alert (inum2); // outputs "-26"
(2) boolean operator
Logic not !, Logical and &, logical or |
(3) Relational operators
VaR bresult = "brick" <"Alphabet ";
Alert (bresult); // outputs "true" // character of B andCodeThey are 66 and 97, respectively.


VaR bresult = "23" <"3 ";
Alert (bresult); // outputs "true"
VaR bresult = "23" <3;
Alert (bresult); // outputs "false" // This is because when the string "23" is compared with the number, the string is converted to the number 23. ***

VaR bresult = "A"> 3;
Alert (bresult); // outputs "false" // This is because the string "A" cannot be converted to a number, while parseint () is called to return Nan .***

8. No overload
However, you can use the arguments object to achieve the same effect, for example:
Function doadd ()
{
If (arguments. Length = 1)
{
Alert (arguments [0]);
}
Else if (arguments. Length = 2)
{
Alert (arguments [0] + arguments [1]);
}
}
Doadd (10); // outputs "10" // we can see that custom functions can accept any number of parameters, ecmascript does not verify whether the number of passed parameters is equal to the number of defined parameters.
Doadd (10, 50); // outputs "60"

9. function class
The most interesting thing is that a function is actually an object!
(1) function sayhi (sname)
{
Alert (sname );
}
// So it can be defined as follows:
VaR sayhi = new function ("sname", "alert (\" Hello \ "+ sname );");
(2) because a function is an object, there are also attributes and methods.
<1> Length attribute, return the number of parameters, for example:
Alert (sayhi. Length); // outputs "1"
<2> valueof () and tostring () methods, return the code of the function, such:
Alert (sayhi. tostring ());

 

 

 

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.