JavaScript Learning Notes (i) Basics _ Basics

Source: Internet
Author: User
Tags numeric value reserved

Basic concepts

javascript是一门解释型的语言,浏览器充当解释器。

Delay script

The defer attribute is defined in HTML4.0.1, and its purpose is to indicate that the script does not affect the construction of the page when it executes. That is, the script is deferred until the entire page is resolved and then executed. Therefore, setting the Defer property in the <script> element is equivalent to telling the browser to download it immediately, but delaying execution. In the XHTML document, you set the Defer property todefer=“defer"

Asynchronous scripts

html5The Async property is defined for <script>. The entire property is similar to the defer property, and is used to alter the behavior of the processing script. Similarly, like defer, Async applies only to external script files and tells the browser to download the files immediately. Unlike defer, however, scripts marked as async do not guarantee that they are executed in the order in which they are specified.
The purpose of the specified async is to not allow the page to wait for the script file to be downloaded and executed, thus asynchronously loading the other content of the page. Therefore, it is recommended that asynchronous scripts do not load during the DOM operation

Case sensitive

Everything in the ECMAScript (including variables, function names, and operators) is case-sensitive.

1. JScript variables

Variables are set in memory the first time they are used, so that they can be referenced later in the script. Declare the variables before using them. You can use the VAR keyword to declare a variable.

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 names are made up of type prefixes + meaningful words, and the hump-style nomenclature 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 divided by type:

S: Represents a String. For example: Sname,shtml
N: Represents a number. For example: npage,ntotal
B: Presentation logic. For example: Bchecked,bhaslogin
A: Represents an array. For example: Alist,agroup
r: Represents a regular expression. For example: Rdomain,remail
f: Represents a function. For example: Fgethtml,finit
o: Represents other objects not covered above, such as: Obutton,odate
g: Represents a global variable, for example: Gusername,glogintime;

JScript is a case-sensitive language. Creating a valid variable name should follow the following rules:

Note that the first character cannot be a number.
Can be followed by any letter or number and underline, but cannot be 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, indicating that next is the variable description, the variable name is the user-defined identifier, and the variables are separated by commas.

If a variable is declared but has not been assigned a value, the variable exists with the value of the JScript value undefined.

Force type conversions

In JScript, you can perform operations on different types of values without worrying about the JSCRIPT interpreter producing 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:

The result

value of the operation is added to the string to  cast the numeric value to a string. The Boolean value is added to the
string  to cast the Boolean value to a string. The value
is added to the Boolean value to  cast the Boolean value to the numeric value.

To explicitly convert a string to an integer, use the parseInt method. To explicitly convert a string to a number, use the parseFloat method.

JavaScriptThe lifetime of a variable: When you declare a variable within a function, you can only access the variable in that function. When the function is exited, 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 variable.
If you declare a variable outside of a function, all functions on the page can access the variable. The lifetime of these variables starts after declaring them, and ends when the page closes.

JS Variable thinking Guide map

2.js data type

JScript has three types of-> primary data types, two-> composite data types, and two-> special data types.

Primary (Basic) data types

字符串数值布尔

Composite (reference) data type

对象数组

Special data types

 Null`Undefined`

String data type: The string data type is used to represent text in JScript. In JS, although double quotes ("") and single quotes (') can represent strings, they are almost indistinguishable. However, only double quotes ("") are used to indicate that the string is considered best.

A string value is a series of 0 or 0 Unicode characters (letters, digits, and punctuation marks) that are queued together.

What is Unicode?

Unicode provides a unique numeric value for each character, regardless of platform, program, or language. Unicode is developed to provide a unified code for handling all the characters that exist in the world.

Numeric data types

We need to understand that JScript internally represents all the values as floating-point values, so there is no difference between integer and floating-point values in JScript.

Boolean data type

Boolean (logic) can have only two values: TRUE or FALSE.

JS Arrays and objects

See my article for details->javascript learning summary--arrays and object parts

Null data type: You can clear the contents of a variable by assigning a null value to a variable.

The operator in JScript typeof will report the null value as a Object type, not a type null .

 
 

nullUsed to indicate an object that does not exist, commonly used to indicate that a function attempted to return an object that does not exist.

UndefinedData type:

The undefined value is returned as follows:

对象属性不存在,声明了变量但从未赋值。

The difference between null and undefined

Alert (typeof undefined); Output "undefined" 
alert (typeof null);//output "Object" 
alert (null = = undefined);//output "true" 

ECMAScript that undefined are derived from null, so they are defined as equal.

alert (null = n = undefined); Output "false" 
alert (typeof null = = typeof undefined);//output "false" 

Null is not the same type as the undefined, so the output is "false". and = = = = Absolute equals, here null = = undefined output false

In addition, here is a more important type of data--reference data type

Reference data type

JavaScript reference data types are objects that are stored in heap memory, and JavaScript does not allow direct access to the location in the heap memory space and the operating heap memory space, only by manipulating the reference address of the object in stack memory. So the reference type of data, stored in the stack memory is actually the object in the heap memory reference address. This reference address allows you to quickly find objects that are saved in heap memory.

Let's demonstrate this reference data type assignment procedure

Naturally, adding the name attribute to the OBJ2 is actually adding the name attribute to the object in the heap memory, and OBJ2 and obj1 are storing only the reference addresses of the heap memory objects in the stack memory, although they are copied, but the same object is pointed to. Therefore, the change of OBJ2 caused obj1 change.

The base type value refers to the simple data segments that are stored in the stack memory, that is, a value that is stored completely in one place in memory.

Reference type values refer to objects that are stored in heap memory, which is actually just 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, namely the original value and the reference value.
Raw values are stored in stack simple data segments in the stack (), that is, their values are stored directly in the location where the variable is accessed. The object that the reference value is stored in the heap (), that heap is, the value stored at the variable is a pointer ( point ) that points to the memory where the object is stored.

<script type= "Text/javascript" >
var box = new Object ();//Create a reference type
var box = "Lee";  The base type value is the string
box.age =;  Basic type value adding attributes are weird because only objects can add properties.
alert (box.age);//is not a reference type and cannot be exported;
</script>

operator of the 3.JScript

Precedence: Refers to the operation order of operators, in layman's terms, which part is calculated first.
Binding: The calculation Order of the same priority operator, in layman's terms, is left to right or right to left.

Data type conversions

String () converted to string type
Number () converted to numeric type
Boolean () Convert to Boolean type

parseInt: Converts a string to an integer. Begins parsing at the beginning of a string, stops parsing at the first non-integer position, and returns all the integers previously read. If the string does not start with an integer, it returns Nan. For example: parseint ("HI") returns the value: 150,parseint ("HI") returns the value: NaN.
parseFloat: Converts a string to a floating-point number. Begins parsing at the beginning of a string, stops parsing at the first non-integer position, and returns all the integers previously read. If the string does not start with an integer, it returns Nan. Such as:parseFloat("15.5 hi") 返回的值是:15.5,parseFloat("hi 15.5")返回的值是:NaN。

eval:将字符串作为javascript表达式进行计算,并返回执行结果,如果没有结果则返回undefined。

4.js Process Control

For JS Process Control statements, here only a few more difficult to understand. Others do not repeat. Please attach a mind map.
The 1.for...in statement corresponds to each of the objects, or to each element of an array, executing one or more statements.

for (variable in [object | array])
statements 

Parameters:

variable: Required option. A variable that can be either a property of object or any element of an array.
object, array : Available options. The object or array to traverse on.
statement: Options available. One or more statements to be executed relative to each attribute of object or to each element of the array. Can be a compound statement.

Although a conditional control statement, such as an if statement, requires the use of a code block (the left curly brace "{" begins with the closing curly brace "}") only if multiple statements are executed, the best practice is to always use code blocks.

if (args)
  alert (args),//error prone

if (args) {
  alert (args);//Recommended
}


JS Process Control Statement Thinking guide map

5.js function

A function is an event-driven or reusable block of code that executes when it is invoked.

JscriptSupports two functions: one is the internal function of the language, the other is created by oneself.
JavaScriptfunctions allow no arguments (but parentheses that contain parameters cannot be omitted) or pass arguments to functions for use.

Learn more about functions please visit my other article: JavaScript learning Summary (iv) function part

The composition of the object

Method--Function: process, dynamic
Property--Variable: state, static

Finally, attach a picture of the previous summary of the mind map:


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.