This section is a quick preview of the JavaScript language and a quick preview of the first part of the book.
After this chapter, we will focus primarily on the basics of JavaScript. In the second chapter we explain JavaScript annotations, semicolons and Unicode character sets, and chapter three will be more interesting, mainly on JavaScript variables and assignments.
Here are some example codes that illustrate the key elements of the first two chapters.
<Scripttype= "Text/javascript"> //After the double slash, the content is in the comments //Read the comments here and it will explain the JavaScript code . //A variable is a symbolic name that represents a value //variables are declared with the VAR keyword varx;//declare a variable x //values can be assigned to variables by symbolsx= 0; //now the value of the variable x is 0.x//gets its value by the variable name. //JavaScript supports multiple data typesx= 1; //Digitalx= 0.01; //integer and real numbers share a data typex= "Hello World"; //string consisting of text inside double quotation marksx= 'Hello World'; //single quotation marks also form strings. x= true; //Boolean valuex= false; //a second Boolean valuex= NULL; //NULL is a special value. It means empty .x=undefined;//undefined and null are very similar </Script>
In JavaScript, the most important types are objects and arrays, the sixth chapter introduces objects, and the 7th chapter describes arrays. Objects and arrays are so important in JavaScript. So that they can be seen everywhere in this book.
<script type= "Text/javascript" >//The most important type in JavaScript is the object //An object is a collection of name/value pairs, or a string value to a collection of mapped values. varBook = {//objects are enclosed in curly braces.Topic: "JavaScript",//the value of the property "topic" is JavaScriptFattrue //the value of the property fat is true};//the curly braces on the right end. //through the "." or "[]" to access the object properties. Book.topic//= "JavaScript"Book["Fat"]//=>true Another way to get properties,Book.author = "AHTHW";//Create a new property by assigning a valueBook.content = {};//{} is an empty object. It has no attributes //JavaScript also supports arrays (lists indexed by arrays) varprimes = [2, 3, 5, 7];//A combination of 4 values, delimited by "[" "]"Primes[0]//=>2: The first object of an array, indexed to 0Primes.length//=>4, the number of elements in the arrayPRIMES[PRIMES.LENGTH-1]//=>7: The last element in the arrayPRIMES[4] = 9;//add a new element by assigning a valuePRIMES[4] = 11;//to change an existing element by assigning a value varempty = [];//an empty array, with 0 elementsEmpty.length//=>:0 //both arrays and objects can contain another array or object. varPoint =[//an array with two elements{x:0,y:0},//Each element is an object{x:1,y:1} ]; varData ={//An object that contains two attributestrial1:[[1,2],[3,4]],//Each of these objects is an arraytrial2:[[2,3],[4,5]]//The elements of the array are also arrays }; </script>
In the code above, the array elements are defined by square brackets and the syntax of the mapping between the object property name and the property value is defined by curly braces (initalizer expression), The fourth chapter has a special introduction. An expression is a phrase in JavaScript that can be used to derive a value from an expression by "," and "[]" to refer to an object property or an array element's value.
The most common expression in JavaScript is an operator like the following code (OPRARTOR)
//operator as an operator to generate a new value //The most common arithmetic operators3+2//=>5 Addition3-2//+ = Subtraction3*2//+ = multiplication3/2//= =Division point[1].x-point[0].x//and complex operations can work as usual"3" + "2"//+ 32. You can complete the addition operation, or you can complete the string concatenation. //JavaScript defines some arithmetic operators as shorthand forms. varCount = 0;//Define a variablecount++;//self-increment 1count--;//self minus 1Count +=2;//self-increment 2 and "Count = Count + 2;" The same way.Count *=3//Squared 3. and "Count = count*3; "The same wayCount//= 6: The variable name itself is an expression //equality relational operators are used to determine whether two values are equal //unequal, greater than, less than operator operation result is true or false varx=2,y=3;//the equals sign here is the meaning of the assignment, not the equivalent .x = = y;//=>false EqualX! = y;//= = True Rangex < y;//= = true: Less thanx <= y;//true is less than or equalx > y;//false greater thanx >= y;//false is greater than or equal"both" = = "three";//false two strings are not equal"Both" > "three";//true "TW" index in the alphabet is greater than "th" false= = (x>y);//ture false =false; //A logical operator is a combination or negation of a Boolean value.(x = = 2) && (y==3);//=>true Two comparisons are true.&& for "and"(x > 3) | | (y<3);//+ = False Two comparisons are not true. | | Represents "or"! (x = = y);//=>true! means negation.
(not to be continued)
Chapter One: JavaScript language core