ECMAScript Basic Knowledge _javascript skills

Source: Internet
Author: User
Tags type casting
One of the core features of JavaScript ECMAScript has many similarities with Java, C, and Perl, many of which are borrowed from these languages, and there are many differences between them. The basic features of some ECMAScript are listed below.

-like Java, ECMAScript is case-sensitive, the annotation is in the same format, the code block is determined by {}, the original data type is stored on the stack, and the object's reference is stored in the heap
--ecmascript is a loose language in which ECMAScript declares a variable through the var operator and is not limited to type, such as var n = 25, then n is the numeric type, var n = "string", then n is the string type
-After each line of code, the ECMAScript automatically considers the end of the line to be the last of the line code, without a semicolon; the variables in ECMAScript can be initialized without initialization, and the system will automatically complete the initialization operation behind the scenes.
--the same variable can give different types of data; The first character of a variable can only be a letter, underscore, or $, and the other characters may be underscores, $, or any number of letters, numbers, characters
-Like other languages, variables are best followed by hump notation, or Pascal notation, or Hungarian notation
Unlike most languages, the ECMAScript variable is not required to be declared before it is used, and the system automatically declares the variable as a global variable, such as var m = "good"; n = m + "Morning"; Alert (n) output structure is "Good Morning"
-In most languages, string is an object, but in ECMAScript it is the original data type

Raw data type

There are five types of ECMAScript raw data: Undefined, Null, Boolean, number, String.

typeof-determines the data type of variables and values, usually with undefined, Boolean, number, String, object five types.
undefined-when a variable is declared but not initialized, or the function does not explicitly return a value, the variable or function is the Undefined type.
Null-undefined is a derivation of NULL, which returns NULL when the value representing an object does not exist.
boolean-contains two values, True and False, false is not equal to 0, but 0 can be converted to false.
Number-can define 32-bit integer data or 64-bit floating-point data. When you define a numeric type variable, the number 0 is octal, plus the 0x is 16, and the results returned by them are unified in decimal. by using var f = 1.0, you can define a floating-point type variable, which, interestingly, is actually stored as a string until the F is used for calculation. When floating-point type data is large or very small (you can move six bits back and forth), you will use the E notation to represent floating-point data, which can store up to 17 bits of data. In addition, the Isfinite () method can determine whether a numerical value is finite, and the isNaN () method can determine a non-numeric type of data.
String-string is the original data type in ECMAScript and is the only data type that does not have a space size limit. Unlike Java, var s = "JavaScript" and var s = ' JavaScript ' are all legitimate representations.

Data conversion

Conversion between different data types is an important feature of any programming language, ECMAScript provides a series of simple ways to implement data conversion, most data types provide a simple conversion method, there are some global methods for complex transformations, either way, Data conversion is very simple in ECMAScript.

The Boolean, number, and string data types are the original data types, but they are also pseudo objects (what exactly should pseudo objects explain in ECMAScript, and how is the operation mechanism unclear?). Someone who knows please give the answer), with its own properties and methods, you can use the ToString () method to implement a string conversion. ECMAScript defines all objects, whether pseudo objects or real objects, can implement the ToString () method, string is listed as a pseudo object of the ranks, nature also has the ToString () method. When converting numeric type data to string, you can add 2, 8, 16 parameters to the ToString () method to implement different data outputs, such as var n = 10; Alert (n.tostring (2)) output is 1010,alert (n.tostring (8)) output is the same as 12,n.tostring () and n.tostring (10).

ECMAScript provides two ways to implement the method of converting a string type to a numeric type: parseint () and parsefloat (). Other type conversions will return NaN (not a number).

Type Casting

Conversions of ECMAScript data types can usually be implemented in three ways: Boolean (value), number (value), and string (value), which usually produce 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
Number (100) 100

String

String () enables direct conversion of all types of data, and unlike ToString (), string () can convert null or undefined data to string.

Reference type

ECMAScript actually does not have a traditional sense of the class, but by defining objects to equate to other languages in the class, which I am still more vague, I may understand later, in the text or "class" to explain.



var ob = new Object ();

The above defines an instance of object, which is similar to Java. Parentheses are needed to refer to arguments when they are available, and parentheses can be removed when no arguments exist. Because the ECMAScript language is loose, whether it is the basic grammar, or the later will refer to the grammatical knowledge, we should try to according to a certain writing specifications to contract their own code format, and should not give full play to the characteristics of language loose.

Object class

The object class is similar to the Java.lang.Object class in Java, which is the base class for all other classes in ECMAScript, and it has the following properties:

constructor-a reference to a function that establishes an object, and for the object class, the reference points to the local object () method.
A reference value for the prototype object in the Prototype-object.

Methods owned by the Object class:

hasOwnProperty-Determines whether the property attribute exists in the object, and the properties data type is string
isPrototypeOf (object)-Determines whether an object is a prototype of another object
propertyIsEnumerable-Determines whether the given attribute can be enumerated using a for statement
ToString ()-Returns the object's original type string
valueof ()-Returns the original value appropriate to the object, and for most classes the returned value is the same as ToString ()
Each property and method of the object class is overridden by another class

Boolean class

Define method var ob = new Boolean (true); OB is a reference to the Boolean original data type.  In the process of using a Boolean object, it is necessary to note that all objects are automatically converted to true, so var ob1 = new Boolean (false); var ob2 = Ob1 && true; The last Ob2 value is true, not false. In general, this can be avoided by using the Boolean raw data type.

Number class

Define method var o = new number (15);
Gets the value of the original data var n = o.valueof ();

The number class has some specially designed methods for values of numeric types:



Alert (o.tofixed (2)); Output 15.00
Alert (o.toexponential (1)); Output 1.5e+1

When you cannot determine whether to use toFixed or toexponential, you can use the Toprecision method to obtain a value:



Alert (o.toprecision (1)); Output 2e+1
Alert (O.toprecision (2)); Output 15
Alert (O.toprecision (3)); Output 15.0

String class

The String class is a complex reference type that lists only a few common methods, many of which are modeled java.lang.String:



var s = new String ("Good Morning");
Alert (s.valueof () = = S.tostring ()); Output 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 be used for string raw data types as well, because they are pseudo objects.

instanceof

The instanceof operator is similar to the TypeOf function, and instanceof needs to explicitly specify whether the object belongs to a particular type. For example



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

Operators and statements

Most of the operators, statements, and Java in ECMAScript are similar, but there are some of them that are unique, such as label statements, with statements, for-in statements, and more.

Functions

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



function functionname (arg0, arg1, ..., argn) {
Statements
}

When a function has no return value or no value after it is returned, the function is actually defined by the system as undefined, and function can not be explicitly specified as a data type when the function returns a value.

About overloading

Overloading is one of the basic features of Object-oriented languages, but ECMAScript functions cannot be overloaded, and in the same scope you can define two identical functions, and the last function works when you call a function. This feature is cumbersome, but it is possible to implement and overload similar functionality by Arguments objects.



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 earlier, two identical functions can be defined in the same scope, and the last function works when the function is invoked.



function func (i) {
Alert (i + 10);
}
function func (i) {
Alert (i + 20);
}
Func (5); Output 25

As you can see, calling the last function makes the data 25, and if you use a function class to define the above two functions, then why use the last function might be more specific.



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 as a reference to a function object, and allows two variables to point to the same function.

There are many properties and methods associated with the function class, such as length, toString (), valueof (), and so on. where ToString () is used more in the debugger.

Original: 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.