The language features of ECMAScript, one of the core of JavaScript, have many similarities with Java, C, and Perl. Many of them are borrowed from these languages, and there are also many differences between them. Here are some basic features of ECMAScript.
-- Like Java, ECMAScript is case sensitive and has the same format of comments. The code block is determined by {}, the original data type is stored on the stack, and the object reference is stored on the heap.
--ECMAScript is a loose language. ECMAScript declares variables through the var operator and does not limit the type. For example, var n = 25, then n is the numeric type, var n = "string", then n is the String type
--After each line of code, you can not write a semicolon. ECMAScript automatically believes that the end of the line is the end of the line of code; variables in ECMAScript can be not initialized, and the initialization operation will be automatically completed in the behind-the-scenes system.
--The same variable can be assigned to different types of data; the first character of the variable can only be a letter, an underscore or $, and other characters can be an underscore, $, or any letter, number, or character.
-- Like other languages, variables should be better followed by camel writing, or Pascal notation, or Hungarian notation.
-- Unlike most languages, the ECMAScript variable does not have to be declared before use. The system will automatically declare 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 a primitive data type
Original data type
There are five types of ECMAScript original data types: Undefined, Null, Boolean, Number, and String.
typeof—The data type that judges variables and values, usually 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 derivative of null. When the value representing an object does not exist, the object returns null.
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 a numeric type variable, adding 0 before the number is octal, adding 0x is hexadecimal, and the result returned after calculation is uniformly decimal. A floating-point type variable can be defined by var f = 1.0. Interestingly, when f is used for calculation, it is actually stored as String type. When the floating-point type data is large or small (can be moved forward and backward by six bits), the E notation will be used to represent the floating-point data, and up to 17 bits of data can be stored. In addition, the isFinite() method can determine whether a numeric value is finite, and the isNaN() method can determine whether a data is a non-numeric type.
String—String is the original data type in ECMAScript and is the only data type without space size limitations. Unlike Java, var s = " javascript " and var s = 'javascript' are both legal representation methods.
Data conversion
Converting between different data types is an important feature of any programming language. ECMAScript provides a series of simple methods to implement data conversion. Most data types provide simple conversion methods, and there are some global methods to complete complex conversions. No matter which method, data conversion in ECMAScript is very simple.
Boolean, number and string data types are primitive data types, but they are also pseudo-objects (how to explain pseudo-objects in ECMAScript and how the running mechanism is still unclear? If someone knows, please give an answer). They have their own properties and methods, and can implement string type conversion through the toString() method. ECMAScript defines all objects, whether they are pseudo-objects or real objects, and can implement the toString() method. String is listed as a row of pseudo-objects, and naturally also has the toString() method. When converting numerical data to string, you can add 2, 8, and 16 parameters to the toString() method to realize data output in different divisions, for example, var n = 10; alert((2)) output is 1010, alert((8)) output is 12, () and (10) are the same.
ECMAScript provides two methods to implement the conversion of string types into numeric types: parseInt() and parseFloat(). Other types of conversion will return NaN (Not a Number).
Type Casting
Conversion of ECMAScript data types can usually be implemented in three ways: 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
Number( 100 ) 100
String
String() can realize direct conversion of all types of data. Unlike using toString(), String() can convert null or undefined data into string.
Reference type
ECMAScript actually does not have classes in the traditional sense, but only equates classes in other languages by defining objects. I am still vague. I may understand later that it is still explained by "classes" in the text.
var ob = new Object();
The above defines an instance of an Object object, and this syntax is similar to Java. When there are parameters, brackets are needed to reference them. When there are no parameters, brackets can be removed. Since the ECMAScript language is relatively loose, whether it is the basic grammar that belongs to or the grammar knowledge mentioned later, we should try to agree on our own code format according to certain writing standards, and should not give full play to the characteristics of loose language.
Object class
The Object class is similar to the classes in Java. It is the base class of all other classes in ECMAScript. It has the following properties:
constructor—A reference to the function that creates an object. For the Object class, this reference points to the local Object() method.
prototype—A reference value of the prototype object in the object.
Methods owned by the Object class:
hasOwnProperty(property)—Judges whether the property property exists in the object, the property data type is string
isPrototypeOf(object)——Prototype to determine whether an object is another object
propertyIsEnumerable(property)— Judge whether the given properties can be listed using the for statement
toString()—Returns the original type string of the object
valueOf()—Returns the original value of the object. For most classes, the returned value is the same as toString().
Every property and method of the Object class is rewritten by other classes.
Boolean class
Definition method var ob = new Boolean(true); ob is a reference to Boolean's original data type. When using Boolean objects, it is necessary to note that all objects will automatically turn to true, so var ob1 = new Boolean(false); var ob2 = ob1 && true; The last value of ob2 is true, not false. In general, using Boolean primitive data types can avoid this.
Number class
Define method var o = new Number(15);
Get the value of the original data var n = ();
The Number class has some methods specially designed for value of numeric types:
alert(( 2 )); // Output 15.00
alert(( 1 )); // Output 1.5e+1
When it is impossible to determine whether to use toFixed or toExponential, you can use the toPrecision method to obtain the value:
alert(( 1 )); // Output 2e+1
alert(( 2 )); // Output 15
alert(( 3 )); // Output 15.0
String class
The String class is a complex reference type. Here are only some common methods, many of which are imitations:
var s = new String( " Good Morning " );
alert(() == ()); // Output true
alert(); // Output 12
alert(( 1 )); // Output o
var sr = ( " ! "); alert(sr); // Output Good morning!
alert(( " o " ); // Output 1
alert(( " o " ); // Output 6
alert((Good morning)); // Output 0
alert((Apple)); // Output 1
alert((House)); // Output-1
alert(( 2 )); // Output od morning
alert(( 2 )); // Output od morning
alert(( 2 , - 5 )); // Output od mo
alert(( 2 , - 5 )); // Output Go
alert(()); // Output GOOD MORNING
alert(()); // Output good morning
In addition, all methods of String class can also be used for String primitive data types because it is a pseudo-object.
instanceof
The instanceof operator and typeof function are similar. The difference is that instanceof needs to specify whether the object belongs to a specific type. For example
var s = new String( " Good morning ! " );
alert(s instanceof String);
Operators and statements
Most operators, statements and Java in ECMAScript are similar, but there are also some unique ones, such as label statements, with statements, for-in statements, etc.
Functions
Functions is the core of ECMAScript, a set of code statements that can be run at any time and anywhere.
function functionName(arg0, arg1, …… , argN) {
statements
}
When the function does not return a value or does not have a value after the return statement, the function will actually be defined by the system as undefined. When the function returns a value, the function does not have to be explicitly specified as a certain data type.
About reload
Overloading is one of the basic features of object-oriented languages, but ECMAScript’s functions cannot be overloaded. Two exactly the same functions can be defined in the same range. When calling the function, the last function will work. This feature is quite troublesome, but it can be used to implement similar functions as overloading through the arguments object.
function func() {
if ( == 1 ) {
alert(arguments[ 0 ] + 5 );
} else if ( == 2 ) {
alert(arguments[ 0 ] + arguments[ 1 ]);
}
}
func( 5 ); // Output 10
func( 10 , 15 ); // Output 25
As mentioned earlier, two exactly the same functions can be defined in the same range. When the function is called, the last function takes effect.
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 so that the data result is 25. If the function class is used to define the above two functions, then why the last function may be more clear.
var func = new Function(“i”, “alert(i + 10 )”);
var func = new Function(“i”, “alert(i + 20 )”);
func( 5 );
func points to another reference, so the value changes. 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(), etc. Among them, toString() is used more frequently in debugging programs.
Original text: /flyingis/archive/2006/06/13/
-- Like Java, ECMAScript is case sensitive and has the same format of comments. The code block is determined by {}, the original data type is stored on the stack, and the object reference is stored on the heap.
--ECMAScript is a loose language. ECMAScript declares variables through the var operator and does not limit the type. For example, var n = 25, then n is the numeric type, var n = "string", then n is the String type
--After each line of code, you can not write a semicolon. ECMAScript automatically believes that the end of the line is the end of the line of code; variables in ECMAScript can be not initialized, and the initialization operation will be automatically completed in the behind-the-scenes system.
--The same variable can be assigned to different types of data; the first character of the variable can only be a letter, an underscore or $, and other characters can be an underscore, $, or any letter, number, or character.
-- Like other languages, variables should be better followed by camel writing, or Pascal notation, or Hungarian notation.
-- Unlike most languages, the ECMAScript variable does not have to be declared before use. The system will automatically declare 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 a primitive data type
Original data type
There are five types of ECMAScript original data types: Undefined, Null, Boolean, Number, and String.
typeof—The data type that judges variables and values, usually 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 derivative of null. When the value representing an object does not exist, the object returns null.
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 a numeric type variable, adding 0 before the number is octal, adding 0x is hexadecimal, and the result returned after calculation is uniformly decimal. A floating-point type variable can be defined by var f = 1.0. Interestingly, when f is used for calculation, it is actually stored as String type. When the floating-point type data is large or small (can be moved forward and backward by six bits), the E notation will be used to represent the floating-point data, and up to 17 bits of data can be stored. In addition, the isFinite() method can determine whether a numeric value is finite, and the isNaN() method can determine whether a data is a non-numeric type.
String—String is the original data type in ECMAScript and is the only data type without space size limitations. Unlike Java, var s = " javascript " and var s = 'javascript' are both legal representation methods.
Data conversion
Converting between different data types is an important feature of any programming language. ECMAScript provides a series of simple methods to implement data conversion. Most data types provide simple conversion methods, and there are some global methods to complete complex conversions. No matter which method, data conversion in ECMAScript is very simple.
Boolean, number and string data types are primitive data types, but they are also pseudo-objects (how to explain pseudo-objects in ECMAScript and how the running mechanism is still unclear? If someone knows, please give an answer). They have their own properties and methods, and can implement string type conversion through the toString() method. ECMAScript defines all objects, whether they are pseudo-objects or real objects, and can implement the toString() method. String is listed as a row of pseudo-objects, and naturally also has the toString() method. When converting numerical data to string, you can add 2, 8, and 16 parameters to the toString() method to realize data output in different divisions, for example, var n = 10; alert((2)) output is 1010, alert((8)) output is 12, () and (10) are the same.
ECMAScript provides two methods to implement the conversion of string types into numeric types: parseInt() and parseFloat(). Other types of conversion will return NaN (Not a Number).
Type Casting
Conversion of ECMAScript data types can usually be implemented in three ways: 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
Number( 100 ) 100
String
String() can realize direct conversion of all types of data. Unlike using toString(), String() can convert null or undefined data into string.
Reference type
ECMAScript actually does not have classes in the traditional sense, but only equates classes in other languages by defining objects. I am still vague. I may understand later that it is still explained by "classes" in the text.
var ob = new Object();
The above defines an instance of an Object object, and this syntax is similar to Java. When there are parameters, brackets are needed to reference them. When there are no parameters, brackets can be removed. Since the ECMAScript language is relatively loose, whether it is the basic grammar that belongs to or the grammar knowledge mentioned later, we should try to agree on our own code format according to certain writing standards, and should not give full play to the characteristics of loose language.
Object class
The Object class is similar to the classes in Java. It is the base class of all other classes in ECMAScript. It has the following properties:
constructor—A reference to the function that creates an object. For the Object class, this reference points to the local Object() method.
prototype—A reference value of the prototype object in the object.
Methods owned by the Object class:
hasOwnProperty(property)—Judges whether the property property exists in the object, the property data type is string
isPrototypeOf(object)——Prototype to determine whether an object is another object
propertyIsEnumerable(property)— Judge whether the given properties can be listed using the for statement
toString()—Returns the original type string of the object
valueOf()—Returns the original value of the object. For most classes, the returned value is the same as toString().
Every property and method of the Object class is rewritten by other classes.
Boolean class
Definition method var ob = new Boolean(true); ob is a reference to Boolean's original data type. When using Boolean objects, it is necessary to note that all objects will automatically turn to true, so var ob1 = new Boolean(false); var ob2 = ob1 && true; The last value of ob2 is true, not false. In general, using Boolean primitive data types can avoid this.
Number class
Define method var o = new Number(15);
Get the value of the original data var n = ();
The Number class has some methods specially designed for value of numeric types:
alert(( 2 )); // Output 15.00
alert(( 1 )); // Output 1.5e+1
When it is impossible to determine whether to use toFixed or toExponential, you can use the toPrecision method to obtain the value:
alert(( 1 )); // Output 2e+1
alert(( 2 )); // Output 15
alert(( 3 )); // Output 15.0
String class
The String class is a complex reference type. Here are only some common methods, many of which are imitations:
var s = new String( " Good Morning " );
alert(() == ()); // Output true
alert(); // Output 12
alert(( 1 )); // Output o
var sr = ( " ! "); alert(sr); // Output Good morning!
alert(( " o " ); // Output 1
alert(( " o " ); // Output 6
alert((Good morning)); // Output 0
alert((Apple)); // Output 1
alert((House)); // Output-1
alert(( 2 )); // Output od morning
alert(( 2 )); // Output od morning
alert(( 2 , - 5 )); // Output od mo
alert(( 2 , - 5 )); // Output Go
alert(()); // Output GOOD MORNING
alert(()); // Output good morning
In addition, all methods of String class can also be used for String primitive data types because it is a pseudo-object.
instanceof
The instanceof operator and typeof function are similar. The difference is that instanceof needs to specify whether the object belongs to a specific type. For example
var s = new String( " Good morning ! " );
alert(s instanceof String);
Operators and statements
Most operators, statements and Java in ECMAScript are similar, but there are also some unique ones, such as label statements, with statements, for-in statements, etc.
Functions
Functions is the core of ECMAScript, a set of code statements that can be run at any time and anywhere.
function functionName(arg0, arg1, …… , argN) {
statements
}
When the function does not return a value or does not have a value after the return statement, the function will actually be defined by the system as undefined. When the function returns a value, the function does not have to be explicitly specified as a certain data type.
About reload
Overloading is one of the basic features of object-oriented languages, but ECMAScript’s functions cannot be overloaded. Two exactly the same functions can be defined in the same range. When calling the function, the last function will work. This feature is quite troublesome, but it can be used to implement similar functions as overloading through the arguments object.
function func() {
if ( == 1 ) {
alert(arguments[ 0 ] + 5 );
} else if ( == 2 ) {
alert(arguments[ 0 ] + arguments[ 1 ]);
}
}
func( 5 ); // Output 10
func( 10 , 15 ); // Output 25
As mentioned earlier, two exactly the same functions can be defined in the same range. When the function is called, the last function takes effect.
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 so that the data result is 25. If the function class is used to define the above two functions, then why the last function may be more clear.
var func = new Function(“i”, “alert(i + 10 )”);
var func = new Function(“i”, “alert(i + 20 )”);
func( 5 );
func points to another reference, so the value changes. 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(), etc. Among them, toString() is used more frequently in debugging programs.
Original text: /flyingis/archive/2006/06/13/