JS-JS implementation, using JavaScript in HTML, basic concepts

Source: Internet
Author: User
Tags bitwise bitwise operators hasownproperty

JS implementation:

1, the composition of JavaScript implementation:

Core (ECMASCRIPT): defined by ECMA-262, providing core language functionality

The Document Object Model (DOM) provides methods and interfaces for accessing and manipulating Web page content

Browser object Model (BOM): Provides methods and interfaces for interacting with the browser

Using JavaScript in HTML

1. <script> elements:

<async>: optional, indicates that the script should be downloaded immediately, but should not interfere with other actions on the page, such as downloading additional resources. But this is only valid for external scripts

<charset>: Optional. The character set that represents the code specified by the SRC attribute

<defer>: Optional. Indicates that the script can be deferred until the document is fully parsed or displayed. Valid only for external script files

<src>: Optional. Represents the external file that contains the code to execute

<type>:

2, the use of <script> two ways:

Use directly in the page;

Eg:<script type= "Text/javascript" >

function say () {

Alert ("</script")

}

</script>

This is a wrong approach and cannot appear in the content now </script> tags

Can be modified by an escape character: alert ("<\/script"), so that the code browser is acceptable and therefore does not cause an error

External files include: <script type= "Text/javascript" src= "Example.js" ></script>;

<script type= "Text/javascript" src= "Example.js"/> This is OK for XHTML, but not in HTML

3, in the introduction of external JS file, between <script></script> can not contain other code, even if there is, will be ignored

4, as long as there is no defer and async attributes, the browser will follow the <script> elements in the order of the page to parse

5. The defer attribute is ignored in HTML5, and the IE8 version supports HTML5 behavior.

6. Browsers that support asynchronous scripting have FIREFOX3.6,SAFARI5 and chrome

Async only supports external script files and tells the browser to download the files immediately, but unlike defer, scripts marked as async do not necessarily follow their order, so it is important to ensure that they are not dependent on each other, and that the purpose of specifying the async attribute is to not allow the page to wait for two scripts to be downloaded and executed. To asynchronously load other content of the page.

7, <noscript> contains content only if the browser does not support script, or browser support script but the script is disabled, it will be displayed

Basic concepts

1, everything in ecmascirpt is case-sensitive

2. Identifiers (Names of variables, functions, attributes, or parameters of functions):

A: The first character must be a letter, underscore, or a dollar sign

B: Other characters can be letters, underscores, dollar signs or numbers

By convention Ecmascirpt the first letter in lowercase, the first letter of each remaining word is capitalized

3, the introduction of strict mode, ECMAScirpt3 some of the indeterminate behavior will be processed, and for some unsafe operations will throw an error, to use strict mode, only need to add code inside the function "use strict"

4. Keywords and reserved words cannot be used as identifiers

Key words:

Reserved words:

Let and yield are the new reserved words in the fifth edition, and Eval and arguments cannot be used as identifiers or property names in strict mode

5, the variable is a loose type, can be used to save any type of data, the definition of variables to use the var operator followed by the variable name

VAR message;

also supports initializing var message= "HI"; it will not flag bit string type

var message= "HI"; is a local variable, the VAR is the global variable

6, Data type: 5 simple data types and a complex data type, do not support the creation of custom data types

5 Simple data types: Undefined, null,boolean,number,string

A complex data type: Object, which is essentially composed of an unordered name-value pair

7. typeof Operator: detects the data type of a given variable and may return one of the following strings for a typeof operator

"Undefined"---if this value is undefined

"Boolean"----if this value is a Boolean type

"String"----if this value is a string

"Number"---if this value is numeric

"Object"---if this value is an object or null

"Function"----if this value is a function

var message= "some";

Alert (typeof message); -----String

Alert (typeof (Message)); -----String

8. Defined type:

First case:

VAR message; The undefined value is obtained by default---

var age does not declare

alert (message); -----undefined

alert (age);------Produce an error

Second case:

VAR message; The undefined value is obtained by default---

var age does not declare

Alert (typeof message); -----undefined

Alert (typeof age);------undefined

Even uninitialized variables are automatically assigned the undefined value

9, NULL

var car=null;

Alert (typeof car); Object type

if (car!=null) {

}

10, Boolean type: True does not necessarily equal 1,false not necessarily equal to 0, but also case-sensitive is true and false is not a Boolean value

var message= "Hello";

var messageasboolean=boolean (message);

If is automatically converted to a Boolean type

var message= "Hello";

if (message) {

Alert ("Value is true! ")

}

11. Type of Number

integers, in addition to decimal notation, can also be represented using 8 hexadecimal literals.

Where the first digit of the octal literal must be 0, then 0~7, if the literal value exceeds the range, then the leading 0 is ignored and the subsequent value is parsed as a decimal.

Hexadecimal literals begin with 0x, followed by 0~9 and A~f, where a~f can be uppercase or lowercase.

A: Floating-point value, must contain a decimal point, after the decimal point contains at least one digit

var floatnum=1. -----There is no number after the decimal point, it is resolved to 1

var floatnum=11.0------Integer, resolved to 11

For large and very small values can be represented by the expression of the floating point in the E-notation, the value of e is equal to the value in front of e multiplied by the exponential power of 10

B: The value range, ECMAScript can be represented by the smallest value saved in Number.min_value, in most of the browser, this is 5e-324, can represent the maximum value of storage number.max_value, in most browsers, This value is 1.7976931348623157e+308. If the result gets an out-of-range value, the value is automatically converted to-infinity (negative infinity) and +infinity (positive infinity). The Infinity value is not able to participate in the calculated numeric value, determines whether a value is not poor, can use the isfinite () function, and returns a true if it is between the minimum and maximum

C:nan. That is, a non-numeric value that represents a case where the operand that would have returned a numeric value does not return a number (so that no exception is thrown), and any value divided by a non-numeric value returns Nan; Nan is not equal to any value, including the Nan itself. The IsNaN () function accepts a parameter of any type, and the function helps us determine whether the parameter is "not a value". Any value that cannot be converted to a number will cause the function to return a true. A positive number divided by 0 returns positive infinity, a negative number divided by 0 returns a negative infinity, and 0 divided by 0 returns Nan.

D: Numeric Conversions: 3 functions convert non-numeric values into numeric values, number (), parseint () to Parsefloat (). Number () can be used for any data type, and another two is dedicated to converting a string into a numeric value.

Number () Conversion rules:

A) If it is a Boolean value, True and false are returned 1,0

b) Null, return 0

c) Undefined, return Nan

d) String: If it contains only numbers, it is converted to decimal values, the string contains a valid hexadecimal format, converts it to the same size decimal value, the string contains a valid floating-point format, and converts it to the corresponding floating-point value; The string is empty and converted to 0 The string contains characters other than the above criteria and converts them to Nan.

e) If it is an object, call the ValueOf () method of the object to convert the returned value according to the preceding rule. If the result is Nan, then the object's ToString method is called, and then the conversion

parseint () Conversion rules:

A) conversion null character will return Nan

b) ignores the spaces in front of the string until the first non-whitespace character is found

var num1=parseint ("1234blue")----1234

var num2=parseint ("")-----NaN

var num3=parseint ("0xA")------10

var num4=parseint ("22.5")---22

var num5=parseint ("070")--56

var num6=parseint ("---") 70

var num7=parseint ("0xf")---15

var num5=parseint ("070", 8)----8 binary number

Parsefloat () Conversion rules:

A) The first decimal point in the string is valid and the second one is invalid

b) The leading 0 is always ignored

c) hexadecimal strings are always converted to 0, only decimal values are parsed

D) If the string contains a numeric value that can be resolved to an integer, parsefloat () returns an integer

E:string types, which can be expressed in single and double quotation marks

F: Convert to String

A) Values, objects, Boolean values, and string values have a tostring method, but null and undefined do not

b) When calling the ToString method color of a number, you can pass a parameter that indicates what type of output data is in the binary

var nam=10;

Alert (num.tostring ()); ----Output "10"

Alert (num.tostring (2)); ---output "1010"

Alert (num.tostring (16)); ----Output "a"

c) without knowing that the converted value is null or undefined, you can use the Transform function string (), which converts any type of value to a string, returns "NULL" if the value is NULL, and returns "If the value is undefined". Undefined

G:object type

a) var o=new Object ();

b) Constructor: Save the function used to create the current object

hasOwnProperty (PropertyName): Used to check whether a given property exists in the current object instance, and the property name as a parameter must exist as a string O. hasOwnProperty ("name")

Ispropertyof (object): Used to check if an incoming object is a prototype of an incoming object

propertyIsEnumerable (PropertyName): Used to check whether a given property can be enumerated using the For-in statement, the parameter property name must be in the form of a string

toLocaleString (): Returns the string representation of an object that corresponds to the region of the execution environment

ToString (): Returns the string representation of an object

ValueOf (): Returns the string, numeric, or Boolean representation of an object, usually the same as the return value of ToString ()

12. Operator

A: Unary operators: Operators that can manipulate only one value

B: Unary plus and minus operators; The unary Plus is not affected by the value, and the unary minus operator is mainly used to denote negative numbers; eg:num=+num;num=-num;

13, bitwise operators: For signed integers, the first 31 bits of 32 bits are used to represent the value of an integer, the 32nd bit is used to represent the symbol of a numeric value, 0 is a positive number, and 1 represents a negative.

Negative numbers are also stored in binary code, but the format used is twos complement

When we output a negative number as a binary string, we see just a minus sign in front of the binary code of the negative absolute value.

A) bitwise non (not) to indicate that the return result is the inverse of the numeric value (the last result is the negative value of the operand minus 1)

b) The bitwise-and (and) is represented by &, which has two operands. Only 1 if both are 1

c) In the case of a bitwise OR (or), there are two operands that return 1 if one bit is 1, and 0 only if two bits are 0.

d) Bitwise XOR (XOR) with two operands, same as 0, different 1

E) left diverted << indicates that this operator shifts all bits of a value to the left by the specified number of digits, the position of the move is supplemented by 0, and the left shift does not affect the sign bit of the operand

var num=2; ----Binary 10

var newnum=num<<5; ----binary 1000000, decimal 64

f) Signed right diverted >> indicates, shifts the value to the right, retains the symbol bit, fills the moving position with the values of the sign bit, to the right of the symbol bit, to the left of the original value

g) Unsigned right diverted >>> indicates that the unsigned right shift operator will use negative binary codes as positive binary codes

14. Boolean Operator: Non (not), with (and), or (or)

A: Logic is not used! Indicates that it can be used for any value in the ECMAScript, regardless of the data type of the value, this operator returns a Boolean value

A) If the operand is an object, return False

b) If the operand is an empty string, returns True

c) operand is a non-empty string that returns false

D) If the operand is 0, returns true

e) If the operand is a non-0 value, including infinity, returns false

f) The operand is null and returns True

g) The operand is Nan and returns true

h) operand is undefined, returns true

B: Logic and with &&, there are two operands, only two are true to return true

A) when there is an operand that is not a Boolean value, the logic and operation do not necessarily return a Boolean value, at which point the following rules are followed

I. If the first operand is an object, the second operand is returned

II. If the second operand is an object, the object is returned only if the value of the first operand evaluates to True

III. If the two operands are objects, the second operand is returned

Iv. if one of the operands is NULL, returns null

V. If one of the operands is Nan, the Nan is returned

VI. If one of the operands is undefined, return undefined

b) in logic and operation, if the first operand is able to make a decision, the second operand is not evaluated

c) If the first operand is false, it is impossible to turn the result to true regardless of the second operand

C: logic or Use | | Indicates that there are two operands, and if there is an operand that is not a Boolean, logic or does not necessarily return a Boolean value, as long as true is true and both are false.

A) If the first operand is an object, the second operand is returned

b) If the evaluation result of the first operand is false, the second operand is returned

c) If two operands are objects, the first operand is returned

D) returns null if all two operands are null

e) If two numbers are Nan, the Nan is returned

f) If two numbers are undefined, return undefined

g) As with logic, the result of the first operand is true, and the second operand is not evaluated

15. Multiplicative operator: multiplication, division, modulo

A: multiplication is represented by a *, and the rules are as follows:

I. Two operands are numeric, performing a regular multiplication calculation, that is, two integers or two negative numbers multiply the result or positive, and only one operand is signed, then the result is a negative number. If the product exceeds the range, the positive or negative infinity is returned

II. If one of the operands is Nan, the result is Nan

Iii. indefinity multiplied by 0 and the result is Nan

Iv. indefinity multiplied by non-0, the result is indefinity or-indefinity, depending on the symbol operand of the symbol

V. indefinity and indefinity multiply, the result is indefinity

Vi. If an operand is not a numeric value, call number () in the background to convert to a number

B: Division is represented by/, and the rules are as follows:

I. Two operands are numeric, performing a regular multiplication calculation, that is, two integers or two negative numbers multiply the result or positive, and only one operand is signed, then the result is a negative number. If the product exceeds the range, the positive or negative infinity is returned

II. If one of the operands is Nan, the result is Nan

III. If the indefinity is indefinity, the result is Nan

Iv. 0 is removed by 0, the result is Nan

V. If a finite number of non-0 is removed by 0, the result is indefinity or-indefinity, depending on the symbol of the signed operand

VI. If indefinity is removed by any non-0 number, the result is indefinity or-indefinity, depending on the symbol of the signed operand

Vii. If an operand is not a numeric value, call number () in the background to convert to a number

C: To find the modulus in%, the remainder, the rule is as follows:

I. Operands are numeric and return the remainder

II. The divisor is an infinite value, and the divisor is a finite value that returns Nan

Iii. the divisor is a finite large value, the divisor is 0, and the result is Nan

Iv. Indefinity was indefinity, the result is Nan

V. If the divisor is a finite number, and the divisor is an infinity value, the result is dividend

VI. The divisor is 0, the result is 0

Vii. If an operand is not a numeric value, call number () in the background to convert to a number

16. Additive operator

A: Addition +

I. An operand is nan and the result is Nan

II. indefinity Plus indefinity, the result is indefinity

Iii.–indefinity plus-indefinity, and the result is-indefinity.

Iv. indefinity plus-indefinity, the result is Nan

V. +0 plus +0, and the result is +0.

Vi.-0 Plus-0, the result is-0

Vii. +0 Plus-0, and the result is +0.

Viii. If all two operands are strings, then the second operand is stitched together with the first operand

IX. If only one operand is a string, the other operand is converted to a string and the two strings are then spelled together

X. For null and undefined, call the string () function to get the strings

B: Subtraction-

I. If two numbers are numeric, the result is returned

II. If one of the operands is Nan, return nan

Iii. indefinity minus indefinity, the result is Nan

Iv.–indefinity minus-indefinity, the result is Nan

V. Indefinity minus-indefinity, the result is indefinity

Vi.–indefinity minus indefinity, and the result is-indefinity.

    1. +0 minus +0, and the result is +0.
    2. +0 minus-0, and the result is-0

Ix.-0 minus-0. The result is +0.

17. Relational operators

A: Less than, greater than, less than or equal to, greater than or equal to

A) If two operands are strings, compare the character encoding values of two strings

b) If one number is numeric, the other operand is converted to a number

c) An operand is an object that calls the ValueOf method or the ToString method

D) If an operand is a Boolean value, convert the its first to a value and then compare

e) Any value compared to Nan, the result is false

18. Equality operator

A: Equality and inequality-first conversion and then comparison; congruent and non-congruent-only compare non-conversions

B: Equality and Inequality: one is a string, one is a value, the string is converted to a value and then compared

null and undefined are equal, cannot convert null and undefined to other values, one operand is Nan, return false, two operands are objects, and if they point to the same object, return True;false equals 0,true equals 1;

C: all equals: with = = =, returns True if two operands are equal without conversion; = = Returns True if the two operands are not equal without conversion;

Null==undefined will return true,null===undefined return False

19. Comma operator: Declare multiple variables in one statement; When used for assignment, the operator always returns the last item in the expression

20. For statement: for (;;) {} infinite loop

21. For-in Statement: The property used to enumerate the object; for (properties in expression) statement

for (var propname in window) {document.write (propname);}----Displays all the properties of the Window object in the BOM, each time the loop is executed, Will assign a property name that exists in the Window object to the variable propname until the enumeration is complete;

When the variable value of an iterated object is null or the undefined,for-in statement does not execute the loop body, it is detected that the value of the object is not null or undefined before using the for-in loop

22, the break statement exits the loop immediately, forcing the execution of the statement following the loop, and the continue statement exits the loop immediately, but resumes from the top of the loop after exiting the loop.

var num=0;

Outermost:

for (var i=0; i<10; i++) {

for (Var j=0;j<10;j++) {

if (i==5 && j==5) {

Break outermost;

}

num++;

}

}

alert (num); -------55

There is a parameter in the break statement for the inner Loop: the label to return to,

The result of adding this tag is to cause the break statement not only to exit the internal for statement (J Loop),

and also exits the external for loop (I loop)

var num=0;

Outermost:

for (var i=0; i<10; i++) {

for (Var j=0;j<10;j++) {

if (i==5 && j==5) {

Continue outermost;

}

num++;

}

}

alert (num); --------95

Continue forces the execution of the loop---exits the inner loop and executes the outer loop.

23. With statement

var qs=location.search.substring (1);

var hostname=location.hostname;

var url=location.href;

With (location) {

var qs=search.substring (1);

var hostname=hostname;

var url=href;

}

Using the WITH statement can result in character degradation

24. Switch (i) {

Case 25:

Case 35;

Alert ("or 35");

Break

Case 45:

Alert ("45");

Break

Default

Alert ("Other");}

25. The switch statement uses the same equality operator when comparing values, and no type conversions occur

26. Functions: Function functionname (ARG0,ARG...ARGN) {statements}

Any code that is behind the return statement will not execute

The return statement can also have no return value, in which case the function returns a undefined value after it has stopped executing. This usage is generally used in cases where the function execution needs to be stopped prematurely without the return value.

Arguments object access parameters, Arguments[0]

function Doadd (num1,num2) {

argument[1]=10;

alert (ARGUMENT[0]+NUM2);

}

ARGUMENT[1] is synchronized with num2, but if the value is passed to a parameter, the value set for argument[1] is not passed to the parameter num2, and the named parameter num2 is automatically assigned undefined

27. In ECMAScript, two functions with the same name are defined, then the name belongs only to the post-defined function.

JS-JS implementation, using JavaScript in HTML, basic concepts

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.