ECMAScript basics _ javascript skills

Source: Internet
Author: User
Tags type casting
The language features of ECMAScript, one of the core knowledge of ECMAScript, have many similarities with Java, C, and Perl. Many of these features are derived from these languages, there are also many differences between them. The following describes some basic features of ECMAScript.

-- Like Java, ECMAScript is case sensitive and the annotation format is the same. The code block is determined by {}, the original data type is stored in the stack, and the object reference is stored in the heap.
-- ECMAScript is a loose language. ECMAScript declares variables through the var operator and has no restrictions on types. For example, if var n = 25, n is a number, var n = "string ", n is of the String type.
-- After each line of code, you can leave the semicolon Unspecified. ECMAScript automatically considers the end of the line as the end of the line of code. Variables in ECMAScript do not need to be initialized. The system will automatically complete the initialization operation behind the scenes.
-- The same variable can be assigned different types of data. The first character of a variable can only be letters, underscores, or $. Other characters can be underscores, $, or any letter, number, or character.
-- Like other languages, variables should best follow the camper writing method, Pascal notation, or Hungary notation.
-- Unlike most languages, the ECMAScript variable does not need to be declared before it is used. The system automatically declares the variable as a global variable, for example, var m = "Good "; n = m + "Morning"; alert (n) output structure is "Good Morning"
-- In most languages, String is an object, but it is the original data type in ECMAScript.

Raw Data Type

ECMAScript has five types of raw data: Undefined, Null, Boolean, Number, and String.

Typeof-data type used to determine the variables and values. Generally, there are five types: undefined, boolean, number, string, and object.
Undefined-when a variable is declared but not initialized, or the function does not explicitly return a value, the variable or function is of the Undefined type.
Null-undefined is a derivation of null. If the value of an object does not exist, null is returned for this object.
Boolean-contains two values: true and false. false is not equal to 0, but 0 can be converted to false.
Number-you can define 32-bit integer data or 64-bit floating point data. When defining numeric type variables, add 0 to the front of the number, that is, octal, and 0x to hexadecimal. After calculation, the returned results are in decimal format. Using var f = 1.0, you can define a floating point variable. Interestingly, f is actually stored as a String type before it is used for calculation. When the floating point data is large or small (six digits can be moved before and after), the E representation is used to represent the floating point data, and up to 17 bytes of data can be stored. In addition, the isFinite () method can determine whether a numerical value is limited. The isNaN () method can determine whether a data type is non-numeric.
String-String is the original data type in ECMAScript and is the only data type with no space limit. Unlike Java, var s = "javascript" and var s = 'javascript 'are both valid representations.

Data Conversion

Conversion between different data types is an important feature of any programming language. ECMAScript provides a series of simple methods to convert data. Most data types provide simple conversion methods, for complex conversions, there are some global methods. No matter which method, the data conversion in ECMAScript is very simple.

Boolean, number, and string data types are original data types, but they are also pseudo objects (in ECMAScript, how do we explain the pseudo objects? How is the running mechanism unclear? You can use the toString () method to convert the string type. ECMAScript defines all objects. Whether it is a pseudo object or a real object, the toString () method can be implemented. A string is listed as a row or column of a pseudo object and naturally has the toString () method. When converting numeric data to string, you can add the 2, 8, and 16 parameters to the toString () method to implement data output in different hexadecimal formats, such as var n = 10; alert (n. toString (2) output is 1010, alert (n. toString (8) output is 12, n. toString () and n. toString (10) is the same.

ECMAScript provides two methods to convert the string type to the numeric type: parseInt () and parseFloat (). For other types, NaN (Not a Number) is returned ).

Type Casting

ECMAScript data type conversion can usually be achieved through three methods: Boolean (value), Number (value) and String (value), which usually produces some unexpected results.

Boolean



Var b1 = Boolean (""); // false-empty string
Var b2 = Boolean ("hi"); // true-non-empty string
Var b3 = Boolean (100); // true-non-zero number
Var b4 = Boolean (null); // false-null
Var b5 = Boolean (0); // false-zero
Var b6 = Boolean (new Object (); // true-object

Number



Number (false) 0
Number (true) 1
Number (undefined) NaN
Number (null) 0
Number ("5.5") 5.5
Number ("56") 56
Number ("5.6.7") NaN
Number (new Object () NaN
No. (100) 100

String

String () can directly convert all types of data. Unlike toString (), String () can convert null or undefined data to string.

Reference Type

ECMAScript does not actually have classes in the traditional sense, but defines objects to be equivalent to classes in other languages. I am still vague and may understand this in the future, in this article, we still use "class" to describe.



Var ob = new Object ();

The above defines an instance of an Object. This syntax is similar to that of Java. When a parameter exists, brackets must be referenced. If no parameter exists, the brackets can be removed. Because the ECMAScript language is loose, whether it is the basic syntax or the syntax knowledge we will mention later, we should try to agree on our own code format according to certain writing specifications, we should not give full play to the loose features of the language.

Object Class

The Object class is similar to the Java. lang. Object Class in java. It is the base class of all other classes in ECMAScript and has the following attributes:

Constructor-creates a reference to an Object function. For an Object class, this reference points to the local Object () method.
Prototype-a reference value of the prototype object in the object.

Methods of the Object class:

HasOwnProperty (property)-determines whether the property exists in the object. The property data type is string.
IsPrototypeOf (object)-determine whether an object is prototype of another object
PropertyIsEnumerable (property)-determines whether the given property can be listed using the for statement.
ToString ()-returns the original type of the object string
ValueOf ()-return the appropriate original value of the object. For most classes, the returned value is the same as toString ().
Every attribute and method of the Object class is overwritten by other classes.

Boolean class

The definition method var ob = new Boolean (true); ob is a reference of the Boolean raw data type. When using a Boolean object, note that all objects are automatically converted to true, so var ob1 = new Boolean (false); var ob2 = ob1 & true; the final value of ob2 is true, not false. This can be avoided by using the Boolean primitive data type.

Number

Define the method var o = new Number (15 );
Obtain the original data value var n = o. valueOf ();

The Number class has some special design methods for numeric values:



Alert (o. toFixed (2); // 15.00 output
Alert (o. toExponential (1); // output 1.5e + 1

When you cannot determine whether toFixed or toExponential is used, you can use the toPrecision method to obtain the value:



Alert (o. toPrecision (1); // output 2e + 1
Alert (o. toPrecision (2); // output 15
Alert (o. toPrecision (3); // 15.0 output

String type

The String class is a complex reference type. Here we only list some common methods, many of which mimic java. lang. String:



Var s = new String ("Good Morning ");
Alert (s. valueOf () = s. toString (); // outputs true
Alert (s. length); // OUTPUT 12
Alert (s. charAt (1); // output o
Var sr = s. concat ("! "); Alert (sr); // output Good morning!
Alert (s. indexOf ("o"); // output 1
Alert (s. lastIndexOf ("o"); // output 6
Alert (s. localeCompare (Good morning); // output 0
Alert (s. localeCompare (Apple); // output 1
Alert (s. localeCompare (House); // output-1
Alert (s. slice (2); // output od morning
Alert (s. substring (2); // output od morning
Alert (s. slice (2,-5); // output od mo
Alert (s. substring (2,-5); // output Go
Alert (s. toUpperCase (); // output GOOD MORNING
Alert (s. toLowerCase (); // output good morning

In addition, all methods of the String class can also be used for the String primitive data type, because it is a pseudo object.

Instanceof

The instanceof operator works similarly to typeof. The difference is that instanceof must specify whether an object belongs to a specific type. For example



Var s = new String ("Good morning! ");
Alert (s instanceof String );

Operators and statements

In ECMAScript, most operators, statements, and Java are similar, but they also have some characteristics, such as label statements, with statements, for-in statements, and so on.

Functions

Functions is the core of ECMAScript and a set of code statements that can be run anywhere at any time.



Function functionName (arg0, arg1 ,...... , ArgN ){
Statements
}

When a function has no return value or no value after a return statement, the function is actually defined as undefined by the system. When a function returns a value, the function does not have to be explicitly specified as a data type.

Reload

Overload is one of the basic features of object-oriented language. However, the functions of ECMAScript cannot be overloaded. Two identical functions can be defined in the same range. When a function is called, the last function plays a role. This feature is troublesome, but you can use the arguments object to implement similar functions as the overload.



Function func (){
If (arguments. length = 1 ){
Alert (arguments [0] + 5 );
} Else if (arguments. length = 2 ){
Alert (arguments [0] + arguments [1]);
}
}

Func (5); // output 10
Func (10, 15); // output 25

As mentioned above, two identical functions can be defined in the same range. When a function is called, The last function plays a role.



Function func (I ){
Alert (I + 10 );
}
Function func (I ){
Alert (I + 20 );
}
Func (5); // output 25

It can be seen that the last function is called to make the data result 25. If the Function class is used to define the above two functions, why is it clearer to use the last function.



Var func = new Function ("I", "alert (I + 10 )");
Var func = new Function ("I", "alert (I + 20 )");
Func (5 );

Func points to another reference, which changes the value. func exists as a reference to the function object and allows two variables to point to the same function.

There are many attributes and methods related to the Function class, such as length, toString (), valueOf (), and so on. Among them, toString () is used in many debugging programs.

Original article: http://www.blogjava.net/flyingis/archive/2006/06/13/52484.html

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.