ECMAScript basic knowledge_javascript skills
The language features of ECMAScript, one of the cores of JavaScript, have many similarities with Java, C, and Perl. Many of the features 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, the format of comments is the same, the code block is determined by {}, the original data type is stored on the stack, and the reference to the object is stored in the heap
--ECMAScript is A loose language, ECMAScript declares variables through the var operator, and the type is not limited. For example, var n = 25, then n is a numeric type, var n = "string", then n is a String type
-- in each After a line of code, you do not need to write a semicolon. ECMAScript automatically considers the end of the line to be 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 to different Type of data; the first character of a variable can only be a letter, underscore, or $, and other characters can be an underscore, $, or any letters, numbers, or characters
- Like other languages, variables are best followed Camel case notation, or Pascal notation, or Hungarian notation
- Unlike most languages, ECMAScript variables do not need 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
Primitive data types
There are five ECMAScript primitive data types: Undefined, Null, Boolean, Number, and String.
typeof - Determines the data type of variables and values. There are usually five types: undefined, boolean, number, string, and object.
Undefined—When a variable is declared but not initialized, or a function does not explicitly return a value, the variable or function is of type Undefined.
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—can define 32-bit integer data or 64-bit floating point data. When defining a numeric type variable, add 0 in front of the number to indicate octal, and add 0x to indicate hexadecimal. The results returned after their calculations are uniformly decimal. A floating-point type variable can be defined by var f = 1.0. Interestingly, before f is used in calculations, it is actually stored as String type. When the floating point type data is large or small (can be shifted forward and backward by six bits), the E notation will be used to represent the floating point data, and a maximum of 17 bits of data can be stored. In addition, the isFinite() method can determine whether a value is limited, and the isNaN() method can determine whether a data is a non-numeric type.
String—String is a primitive data type in ECMAScript and is the only data type that has no space size limit. Unlike Java, var s = "javascript" and var s = 'javascript' are both legal expression methods.
Data conversion
Converting between different data types is an important feature of any programming language. ECMAScript provides a series of simple methods to achieve data conversion for most data types. All provide simple conversion methods, and there are some global methods to complete complex conversions. No matter which method is used, 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? How is the operating mechanism still unclear? If anyone knows, please give an answer), with Your own properties and methods can be converted to string type through the toString() method. ECMAScript defines that all objects, whether pseudo-objects or real objects, can implement the toString() method. String is listed as a pseudo-object, and naturally also has a toString() method. When converting numeric data to string, you can add 2, 8, and 16 parameters to the toString() method to realize data output in different bases, 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) are the same.
ECMAScript provides two methods to convert string type into numeric type: parseInt() and parseFloat(). Other type conversions will return NaN (Not a Number).
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 result.
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, and The difference with using toString() is that String() can convert null or undefined data into string.
Reference type
ECMAScript actually does not have classes in the traditional sense. It just defines objects to be equivalent to classes in other languages. I am still vague about this point. I may understand it in the future. In the article, it is still explained by "category".
var ob = new Object();
The above defines an instance of Object object. This syntax is similar to Java. When there are parameters, parentheses are needed to quote. When there are no parameters, the parentheses can be removed. Since the ECMAScript language is relatively loose, whether it is the basic grammar mentioned above or the grammatical knowledge mentioned later, we should try our best to agree on our own code format according to certain writing standards, and should not give full play to the loose characteristics of the language.
Object class
The Object class is similar to the java.lang.Object class in Java. It is the base class for all other classes in ECMAScript. It has the following attributes:
Constructor—a reference to a 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) - Determine whether the property attribute exists in the object, the property data type is string
isPrototypeOf(object) - Determine whether an object is The prototype of another object
propertyIsEnumerable(property) - Determines whether the given property can be enumerated using the for statement
toString() - Returns the original type string of the object
valueOf() - Returns the appropriate value of the object Primitive value. For most classes, the returned value is the same as toString()
Every property and method of Object class is overridden by other classes
Boolean class
Define method var ob = new Boolean(true); ob is a reference of Boolean primitive data type. When using Boolean objects, you need to note that all objects will automatically change to true, so var ob1 = new Boolean(false); var ob2 = ob1 && true; Finally, the value of ob2 is true, not false. In general, this situation can be avoided by using the Boolean primitive data type.
Number class
Define method var o = new Number(15);
Get the value of the original data var n = o.valueOf();
Number class There are some methods specially designed for numeric values:
alert(o.toFixed( 2 )); // Output 15.00
alert(o.toExponential( 1 )) ; // Output 1.5e 1
When you are unsure whether toFixed or toExponential should be used, you can use the toPrecision method to get the 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. Here are only some common methods, many of which imitate 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 The methods of the String class can also be used on the String primitive data type because it is a pseudo object.
instanceof
The instanceof operator is similar to typeof. The difference is that instanceof needs to explicitly 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 and statements in ECMAScript are similar to Java, but there are also some unique ones, such as label statement, with statement, for-in statement, etc.
Functions
Functions are 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 function has no return value or there is no value after the return statement , the function will actually be defined as undefined by the system. When the function returns a value, the function does not need to be explicitly specified as a certain data type.
About overloading
Overloading is one of the basic features of object-oriented languages, but ECMAScript functions cannot be overloaded. Two identical functions can be defined in the same scope. When a function is called, the last function comes into play. This feature is cumbersome, but similar functions to overloading can be implemented through 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. 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 It might be clearer why the last function is used.
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 often used in debugging programs.
Original text: http://www.blogjava.net/flyingis/archive/2006/06/13/52484.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



es2017 is es8. The full name of es is "ECMAScript", which is a universal scripting language implemented according to the ECMA-262 standard. The version officially released in June 2017 is officially called ECMAScript2017 (ES2017). Because it is the 8th version of ECMAScript, it can Referred to as es8.

This article will sort out and share with you the features of ECMAScript. It will take you an hour to quickly understand all the features of ES6~ES12. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

MySQL is an open source relational database management system that is widely used in web application development and data storage. Learning MySQL's SQL language is very necessary for data managers and developers. SQL language is the core part of MySQL, so before learning MySQL, you need to have a full understanding of SQL language. This article aims to explain the basic knowledge of SQL statements to you in detail, so that you can understand SQL statements step by step. SQL is the abbreviation of Structured Query Language, which is used in relational data

Starting from Scratch: Introduction to the Basics of Learning Go Language Go language, also known as Golang, is an open source programming language developed by Google. It was released in 2009 and quickly became a popular language, especially in fields such as web development, distributed systems, and cloud computing. The Go language is famous for its simplicity, efficiency, and strong concurrency. Basic syntax 1. Variables and constants In the Go language, variables and constants are typed. Variables can store data, while constants cannot be changed. The declaration format of variables is: v

Yii is a popular object-oriented PHP framework. Its full name is "YesItIs", which means "Yes, it is like this". It is designed to be efficient, fast, secure and easy to use, so it is widely used in the development of large-scale web applications. In this article, we will introduce some basic knowledge of Yii framework to help novices better understand this framework. MVC architecture Yii framework adopts a design pattern based on MVC (Model-View-Controller), which

What basic knowledge do you need to learn canvas? With the development of modern web technology, using the <canvas> tag in HTML5 for drawing has become a common way. Canvas is an HTML element used to draw graphics, animations and other images, which can be manipulated and controlled using JavaScript. If you want to learn canvas and master its basic knowledge, the following will introduce it to you in detail. HTML and CSS basics: before learning canvas

Quick Start Go Language Programming: Basic Knowledge and Practice Guide Go language, as an emerging programming language, is favored by developers for its simplicity, efficiency and concurrency. Whether you are a beginner or a developer with some programming experience, this article will take you quickly into Go programming and provide some practical guidelines and specific code examples. 1. Install the Go language environment. To start programming in the Go language, you first need to install the Go language environment on your computer. You can download it from Go official website (https://golang

"In-depth Discussion on the Basics of Go Language Programming: Analysis of Specific Code Examples" As a fast and efficient programming language, Go language is increasingly favored by programmers and developers. In the process of learning and mastering the Go language, it is crucial to have a deep understanding of its basics. This article will conduct an in-depth discussion on variables, data types, process control, functions, etc., and combine it with specific code examples to help readers better understand and master the basic knowledge of the Go language. Variables and data types In Go language, the declaration and initialization of variables are very
