Transferred from: http://segmentfault.com/a/1190000000652749
Basic concepts
Delay script
A property is defined in HTML4.0.1 defer , which is used to indicate that the script does not affect the construction of the page when it executes. In other words, the script is deferred until the entire page has been parsed and then executed. Therefore, setting a property in an <script> element is defer equivalent to telling the browser to download it immediately, but defer execution. In the XHTML document, to set the defer property to defer=“defer" , currently only Internet Explorer supported defer properties.
Asynchronous script
html5The <script> attribute is defined for async . The entire property is similar to the defer property and is used to change the behavior of the processing script. Similarly, defer similar, async applies only to external script files and tells the browser to download the file immediately. However defer , unlike the scripts that are marked, async they are not guaranteed to be executed in the order in which they are specified.
asyncThe purpose is to not allow the page to wait for the script file to be downloaded and executed to asynchronously load the other content of the page. Therefore, it is recommended that asynchronous scripts do not load during operationsDOM
Case sensitive
ECMASCriptAll (including variables, function names, and operators) are case-sensitive.
1. Variables
Variables are set in memory the first time they are used, making it easier to refer to them later in the script. Declare the variables before using them. You can use var keywords to make variable declarations.
var count, amount, level; // 用单个 var 关键字声明的多个声明。
Variable naming
Variable names include global variables, local variables, class variables, function parameters, and so on, and they all fall into this category.
Variable naming is made up of type prefixes + meaningful words, and the use of camel-named methods increases the readability of variables and functions. For example:sUserName,nCount。
Prefix specification:
Each local variable needs to have a type prefix, which can be categorized by type:
s:表示字符串。例如:sName,sHtml;n:表示数字。例如:nPage,nTotal;b:表示逻辑。例如:bChecked,bHasLogin;a:表示数组。例如:aList,aGroup;r:表示正则表达式。例如:rDomain,rEmail;f:表示函数。例如:fGetHtml,fInit;o:表示以上未涉及到的其他对象,例如:oButton,oDate;g:表示全局变量,例如:gUserName,gLoginTime;
JScriptis a case-sensitive language. Creating a valid variable name should follow these rules:
Note that the first character must not be a number.
Can be followed by any letter or number as well as an underscore, but not a space
The variable name must not be a reserved word.
javascriptis a weakly typed language that JavaScript ignores extra spaces. You can add spaces to the script to improve its readability.
varis javascript the reserved word, which indicates that the variable description is followed by a user-defined identifier, separated by commas.
If a variable is declared but not assigned a value, the variable exists and its value is a Jscript value undefined .
Forcing type conversions
In Jscript , you can perform operations on different types of values without worrying about the JScript interpreter generating an exception. Instead, the JScript interpreter automatically changes one of the data types (casts) to another data type, and then performs the operation. For example:
运算 结果数值与字符串相加 将数值强制转换为字符串。布尔值与字符串相加 将布尔值强制转换为字符串。数值与布尔值相加 将布尔值强制转换为数值。
To explicitly convert a string to an integer, use the parseInt method. To explicitly convert a string to a number, use the parseFloat method.
JavaScriptLifetime of a variable: After you declare a variable within a function, you can only access the variable in that function. When you exit the function, the variable is revoked. This variable is called a local variable. You can use local variables with the same name in different functions, because only the function that declares the variable can recognize each of these variables.
If you declare a variable outside of a function, all functions on the page can access the variable. The lifetimes of these variables begin after they are declared and end when the page closes.
JS Variable Mind Mapping
Data type of 2.js
There are three types of JScript, the main data types, the two composite data types, and the two special data types.
Primary (Basic) data types
字符串数值布尔
Composite (reference) data types
对象数组
Special data types
Null`Undefined`
String data type: The string data type is used to represent JScript the text in. In js , although double quotation marks ("") and single quotation marks (' ') can represent strings, and they are almost indistinguishable. But using only 双引号 ("") to represent the string is considered best.
A string value is a series of 0 or more Unicode characters (letters, numbers, and punctuation marks) that are lined together.
What is Unicode?
UnicodeProvides a unique value for each character, regardless of platform, program, or language. Developed unicode to provide a uniform encoding for all characters that exist in the world.
Numeric data type
We need to understand that JScript internally all values are represented as floating-point values, so Jscript there is no difference between the integers and floating-point values.
Boolean data type
Boolean (logic) can have only two values: true or false .
JS Arrays and objects
See my article->javascript study summary--array and object parts
null data type: You can clear the contents of a variable by assigning a null value to a variable.
JscriptThe typeof operator in the report null value is a Object type, not a type null .
<Htmlxmlns="Http://www.w3.org/1999/xhtml"xml:lang="EN" ><Head><Metahttp-equiv= "Content-type" content= "text/ Html;charset=utf-8 "/> <title> </title> <script type=" Text/javascript "> alert (typeof null); </script></ head><body> </body></ HTML>
nullUsed to represent an object that does not already exist and is commonly used to represent a function attempt to return an object that does not exist.
UndefinedData type:
The following conditions return the undefined value:
对象属性不存在,声明了变量但从未赋值。
The difference between null and undefined
alert(typeof undefined); //output "undefined" alert(typeof null); //output "object" alert(null == undefined); //output "true"
ECMAScriptConsidered to undefined be null derived from, so they are defined as equal.
alert(null === undefined); //output "false" alert(typeof null == typeof undefined); //output "false"
nullundefinedis not the same as the type, so Output " false ". and the = = = is absolutely equal to the null === undefined output herefalse
In addition, here is a more important data type--reference data type
Reference data type
javascriptReference data types are objects that are stored in heap memory, JavaScript do not allow direct access to the location in the heap memory space, and manipulate heap memory space only by manipulating the object's reference address in the stack memory. So referencing the type of data that is stored in the stack memory is actually the object's reference address in the heap memory. This reference address allows you to quickly find objects that are stored in heap memory.
Let's demonstrate this reference data type assignment process
Naturally, to obj2 add name attributes, you actually add properties to the objects in the heap memory, name obj2 and obj1 only the reference addresses of the heap memory objects that are stored in the stack memory, although they are copies of one copy, but point to the same object. So obj2 the change caused by the change obj1 .
Primitive Type Values refer to simple data segments that are stored in the stack's memory, meaning that this value is completely stored in a single location in memory.
The reference type value refers to the object that is stored in the heap memory, that is, the variable is actually a pointer to another location in memory where the object is saved.
In short, heap memory holds reference values, and stack memory holds fixed type values.
In ECMAScript , a variable can have two types of values, the original value and the reference value.
Primitive values are stored in simple data segments in the stack ( stack ), that is, their values are stored directly in the location where the variable is accessed. The reference value is stored in the object in the heap (), that heap is, the value stored at the variable is a pointer ( point ) to the memory where the object is stored.
<script type="text/javascript”>var box = new Object(); //创建一个引用类型var box = "lee"; //基本类型值是字符串box.age = 23; //基本类型值添加属性很怪异,因为只有对象才可以添加属性。alert(box.age); //不是引用类型,无法输出;</script>
Operators for 3.JScript
Precedence: Refers to the operator's order of operations, which is popularly said to be the first to calculate which part.
Binding: The order of calculation of the same precedence operator, in which direction it is calculated from, left to right or right to left.
Data type conversions and basic wrapper types
String()Convert to String type
Number()Convert to numeric type
Boolean()Convert to Boolean type
parseInt: Converts a string to an integer. Parsing begins at the beginning of the string, stops parsing at the first non-integer position, and returns all integers that were previously read. If the string does not start with an integer, Nan is returned. For example, the value returned by parseint ("150,parseint") is: NaN.
parseFloat: Converts a string to a floating-point number. Parsing begins at the beginning of the string, stops parsing at the first non-integer position, and returns all integers that were previously read. If the string does not start with an integer, Nan is returned. Such as:parseFloat("15.5 hi") 返回的值是:15.5,parseFloat("hi 15.5")返回的值是:NaN。
eval:将字符串作为javascript表达式进行计算,并返回执行结果,如果没有结果则返回undefined。
Basic Package Type
Whenever a primitive type value is read, the background creates an object of the corresponding basic wrapper type, which can invoke some methods to manipulate the data. Basic package types include Boolean , Number andString
var box =' TRIGKIT4 ';//literal box.name = ' Mike '; //Invalid property Box.age = function () {//Invalid method return 22;}; //new operator notation var box = new string ( ' TRIGKIT4 '); ' Mike '; //valid attribute Box.age = function () {//effective method return 22;
StringType contains three properties and a large number of available built-in methods
属性 描述length :返回字符串的字符长度Constructor : 返回创建String对象的函数prototype : 通过添加属性和方法扩展字符串定义
4.js Process Control
For JS flow control statements, here are only a few more difficult to understand. Others do not repeat. A mind map is attached.
The 1.for...in statement corresponds to each of an object, or to each element of an array, to execute one or more statements.
for (variable in [object | array])statements
Parameters:
variable: Required option. A variable that can be any of the properties of an object or any element of an array.
object, array : Optional. The object or array on which to traverse.
statement: Available options. One or more statements that are to be executed relative to each property of object or to each element of an array. Can be a compound statement.
Although a conditional control statement, such as an if statement, requires a code block (ending with the opening curly brace "{", the right curly brace "}") only when executing multiple statements, the best practice is to always use code blocks.
if(args) alert(args);//容易出错if(args){ alert(args);//推荐使用}
JS flow control Statement Mind Mapping
5.js function
A function is an event-driven or reusable block of code that executes when it is invoked.
JscriptTwo functions are supported: one is a function within a language, and the other is created by itself.
JavaScriptThe function allows no arguments (but the parentheses that contain the arguments cannot be omitted), or you can pass parameters to the function for use by the function.
Learn more about functions please visit one of my other articles: JavaScript Learning big Summary (iv) Function functions section
The composition of the object
方法——函数:过程、动态的属性——变量:状态、静态的
Finally, attach a mind map of the predecessors ' summary:
JavaScript Learning Summary (i) Basic section