JavaScript DOM Programming Art Learning Notes (ii)

Source: Internet
Author: User
Tags arithmetic operators integer numbers scalar uppercase letter

Chapter II JavaScript syntax
2.1preparing the environment for writing javascript: text editor+code written by a Web browser in JavaScript must be HTML-The/xhtml document can be executed. There are two ways to do this.The first is to put JavaScript code in the documentbetween Tags:<!    DOCTYPE html>JavaScript goes here ...</script>Mark-Up goes ...</body>Note and point its SRC attribute to the file:<!    DOCTYPE html>Mark-Up goes ...</body>Mark-Up goes ...<script src= "File.js" ></script></body>This allows the browser to load the page more quickly.3. The programming language is divided into two categories: explanatory type and compiler type. Compiled by Java,c++and so on, the compiler (compiler) is required to translate the source code into a file that executes directly on the computer. Pros: Code written in a compiled language has errors that can be discovered during code compilation. Faster and more portable than ever. Explanatory: JavaScript, etc., No compiler required-----They only need an interpreter. In the JavaScript language, the Web browser is responsible for interpreting and performing the work in the Internet environment. The JavaScript interpreter in the browser is read directly into the source code and executed. If there is no interpreter in the browser, JavaScript code cannot be executed. Pros: Easy to get started. Cons: Errors in code that can only be discovered when the interpreter executes to the relevant code .2.2Grammar2.2.1scripts written in JavaScript, like scripts written in other languages, are composed of a series of instructions called statements (statement). Good programming habits: add a semicolon at the end of each statement, first statement; Second statement;2.2.2Notes<!--This is a comment in JavaScript.<!--This is an annotation of the HTML type--Recommendation: Use"//" to comment on a single line, with/*XXX*/comment multiple lines.2.2.3Variable variable: The variable (variable). For example: Age. Assignment: The operation of storing a value in a variable is called an Assignment (Assignment). Once the variable is assigned, we say the variable contains the value. Mood= "Happy"; Age= 33; In a JavaScript script, if the programmer is not declared before assigning a value to a variable, the assignment automatically declares the variable. Although JavaScript does not mandate that programmers must declare variables in advance, declaring them in advance is a good programming habit.varMood,age;mood= "Happy"; Age= 33; orvarMood = "Happy";varAge = 33; orvarMood = "Happy",varAge = 33in the JavaScript language, the names of variables and other grammatical elements are case-sensitive. JavaScript syntax does not allow a variable name to contain spaces or punctuation marks (dollar sign"$"exceptions). JavaScript variable names allow for letters, numbers, dollar signs, and underscores (but the first character is not allowed to be a number). Another way is to use the hump format (Camel Case), remove the middle white space (underscore), followed by a new word with the beginning of the uppercase letter; Usually the hump format is the preferred format for function name, method name, and object property name naming.varMymood = "Happy"in the above statement, the word"Happy"is a literal in the JavaScript language (literal), which is data that can be written directly in JavaScript code.2.2.4data Type type declaration (typing): When declaring a variable, you must also declare the data type of the variable, which is known as a type declaration. Strongly typed (strongly typed) language must be explicit type declaration, weak type (weakly typed) language is not required, JavaScript doesn't need to be. String strings are composed of 0 or more characters. Characters include (but are not limited to) letters, numbers, punctuation, and spaces. The string must be enclosed in quotation marks, either single or double quotes. The following two statements have the exact same meaning:varMood = ' happy ';varMood = "Happy"if you want to wrap a string containing double quotes in double quotation marks, you must escape the double quotes in the string with a backslash (escaping). The characters are escaped in JavaScript with backslashes:varHeight = "About 5 ' 10\" tall "; single quote:varMood = ' don\ ' t ask 'as a good good programming habit, regardless of whether you choose to use double or single quotes, keep it consistent throughout the script. Double quotation marks are recommended.2. NumericvarAge = 33.25;3. Boolean Boolean value (Boolean) data has only two optional values----true or False.varSleeping =true;2.2.5array strings, numeric values, and Boolean values are scalar (scalar). If a variable is a scalar, it can have only one value at any time. If you want to store a set of values with a variable, you need to use an array.1An array is a variable that represents a worthwhile collection, and each value in the collection is an element of the array.varBeatles = Array (4); beatles[0] = "John"; beatles[1] = "Paul"; beatles[2] = "George"; beatles[3] = "Ringo;"orvarBeatles = Array ("John", "Paul", "George", "Ringo")), orvarBeatles = ["John", "Paul", "George", "Ringo"]; element type not requiredvarLennon = ["John", 1940,false]; An array element can also be a variable:varName = "John"; beatles[0] =name; The value of an array element can also be an element of another array:varnames = ["Ringo", "John", "George", "Paul"];beatles[1] = Names[3]; Arrays can also contain other arrays: any of the elements in an array can have an array as its value;varLennon = ["John", 1940,false];varBeatles =[];beatles[0] =Lennon;2. Associative arrays If you only give the value of the element when the array is populated, the array will be a traditional array, and the subscripts of each element will be automatically created and refreshed. This default behavior can be changed by explicitly giving a subscript to each new element when populating an array. When you give the subscript for a new element, You do not have to limit the use of integer numbers.varLennon =Array (); lennon["Name"] = "John"; lennon["Year"] = 1940; lennon["Living"] =falsesuch an array is called an associative array.2.2.6Objects are similar to arrays, and objects use a name to represent a set of values. Each value of an object is a property of an object. Create object {Propertyname:value,propertyname:value} example:varLennon = {name: "John", year:1940,living:false }2.3Manipulation arithmetic operators: assignment operators=addition operator+subtraction operator-multiplication operator*Division operator/Plus (+) is a special operator that can be used for both numeric and string characters.varTotal = (1 + 4) * 5;varMessage = "I am Feeling" + "Happy"; The operation of putting multiple strings together, like this, is called stitching (concatenation). Because JavaScript is a weak language, you can stitch strings and values together, and values are automatically converted to strings:varYear = 2016;varMessage = "The year is" +Year ; If you stitch together a string and a value, the result will be a longer string, but if you use the same operatorStitchingtwo values, the result will be the arithmetic of the two values and the. Alert ("10" + 20);//returns the string "1020"Alert (10 + 20);//return value2.4conditional Statementsif(condition) {statements;}2.4.1comparison operator is greater than>less than<equals==Not equal to!=greater than or equal to>=less than or equal to<=congruent operator===strictly comparing, not only comparing values, but also comparing the types of variables; inequality operators!==var=false;var= "";if(A = = =b) {alert ("A equals B");}2.4.2Logical operator Logic and&&Logical OR||Logical Non-!2.5Looping Statements2.5.1While Loop while(condition) {statements;} As long as the evaluated result of the given condition is true, the code contained in the curly braces will be executed over and over again. Do... while loop Do{statements;} while(condition); statements contained in curly braces are executed at least once2.5.2For Loop for(initial condition;test condition;alter conditon) {statements;} Example: for(varCount = 1;count < 11;count + +{alert (count);}2.6The function defines the syntax of a function:functionname (arguments) {statements;} When you define a function, you can declare any number of arguments for it, as long as you separate them with commas.functionMultiply (num1,num2) {varTotal = Num1 *num2; Alert (total);} Multiply (10,2); You can also use a function as a data type, which means that you can assign a function's invocation result to a variable:varTemp_fahrenheit = 95;varTemp_celsius =Converttocelsius (Temp_fahrenheit), alert (Temp_celsius), scope of the variable: global variable (globals variable) Can be referenced anywhere in the script. Once a global variable is declared in a script, it can be anywhere in the script.----include internal functions----refer to it. The scope of the global variable is the entire script. The local variable (locally variable) exists only inside the function that declares it, and cannot be referenced outside of that function. Local variables are scoped to a specific function. If you use Var in a function, The variable is treated as a local variable that exists only in the context of the function, whereas if VAR is not used, that variable is treated as a global variable, and if a global variable with the same name already exists in the script, the function changes the value of that global variable. Example:functionSquare (num) { total= num *num; returnTotal ;}varTotal = 50;varNumber = Square (20); alert (total);//The output is 400, and the value of global variable total is changed to.ModifyfunctionSquare (num) {varTotal = num *num; returnTotal ;} Now that the global variable total becomes secure, then how to move the square () function will not affect it. The function should be more like a self-sufficient script in terms of behavior, and when defining a function, we must explicitly declare its internal variables as local variables.2.7An Object object is a very important data type. The object is a self-contained collection of data, and the data contained in the object can be accessed in two ways------property and method. A property is a variable that is subordinate to a particular object, which is a function that only a particular object can invoke. An object is a data entity that is composed of properties and methods. In JavaScript, Properties and methods are usedPointsyntax to access: Object.propertyObject.method ()2.7.1The built- in object JavaScript provides a set of pre-defined objects that can be used to refer to objects that are built-in (native object).varBeatles =NewArray ();2.7.2Host Objects In addition to the built-in objects, you can also use some predefined objects in JavaScript scripts. These objects are not provided by the JavaScript language itself but by its operating environment. Specific to Web applications,                                                                                                                   This environment is the browser. The predefined objects provided by the browser are referred to as host objects (host object). The Host object includes form, image, Element,document, and so on. 

JavaScript DOM Programming Art Learning Notes (ii)

Related Article

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.