[JS] JavaScript basic syntax

Source: Internet
Author: User
Tags tagname

What is 1.javascript?

JS is an interpretive programming language with object-oriented capability.

Type of 2.js

[Base type]:string Number Boolean

[Composite Type]: Object (object, function, array)

[No type]:null undefiend

3.typeof

Syntax: string typeof (variable name) or string typeof variable name

Function: Returns the type of the variable

4. Block-level scopes

JS does not have the concept of block-level scopes. Variables declared inside an if while are the same scope as variables declared outside.

5. Operators

= = and! =: is to determine whether the 2 results are equal

In: Whether the property belongs to an object

instanceof: Whether a variable belongs to a class add instanceof Function

6. Number types

The number types in JS are floating-point numbers. 1===1.00

8 binary: 0 start 16 binary: 0x start

  

7.NaN Infinity IsNaN Isfinite

Nan:not a number alert (0/0)

Infinity: Infinite alert (0.1/0)

Boolean IsNaN (number): Judging is not a digit

Boolean isfinite (number): Determine if the numbers are limited

8.string Type method

String substring (Startindex,endindex): To the left, not to the right

String substr (Startindex,length):

  

9. Package Type

The use of wrapper types and basic types has always been an extension of the base type.

Number String Boolean

10. Functions/Anonymous functions

function is the soul of JS, the anonymous function is to assign a function to a variable.

Functions: Function name (parameter) {method body}

anonymous function: var variable name = function (parameter) {method body}

11. Variable name promotion/function name promotion

The function is a local scope, and a variable declared using var within a local scope implicitly has a predecessor.

Such as:

var i = 1;

function Test () {

alert (i);

var i = 2; The I declared here is equivalent to being declared at the top of the local scope. So I for alert (i) is undefined.

alert (i);

}

var func = function () {return 1};

function Test () {

Func ();

var func = function () {return 2}; This is the same as the previous example, because it is the reason for the function call. There will be a direct error. Do not go on.

Func ();

}

12. Dynamic functions

New function (Arg1,arg2...argn,body): Creates and returns an

13.arguments

Inside the function there will be a default arguments object, not an array.

typeof arguments = Object. Arguments instanceof Array = False

14. Create an Object

There is no concept of class in JS, but functions can be used as constructors. The object is actually a key-value pair.

var person = function () {

This.id = 1;

This. Name = ' 2 ';

}

var p1 = new Person (); A person object has been created with the property ID and name

Principle:

New keyword: Creates an object. The This object is pointed to in the function.

Constructor: The execution constructor is the this extension member. Returns the This object after execution.

15. The literal value of the object

var o = {

Id:1

, Name: ' 2 '

}

You can add member O as you wish. Age = 3; Delete Member: delete o.age; call member o[' age '] ();

16. Creation of arrays

2 Ways to create:

var arr = new Array ()

var arr = []

The general assignment to an array is:

Arr[arr.length] = value;

17.null undefined

NULL indicates NULL, no reference object.

Undefined represents undefined and does not exist. There are 3 situations:

A. Undefined properties

B. Undefined variables

C. Returns only return

  

18.Error

Equivalent to a exception object in C #

Throw new error in JS (' Error here ');

Common: Throw {msg: ' exception ', Date:new date ()}

19. Common functions

Encoding function:

encodeURI: String encoding, so that Chinese can be transferred to the normal transmission network recommended use

decodeURI: Decodes the string. Recommended Use

Escape: Encoded UNESCAPE: decoded encoded into 16 binary.

20. Extension methods

The essence of an extension method is inheritance.

Constructors all have prototype (prototype) properties. The object through the constructor new is actually derived from prototype.

Note: All functions inherit from function. indirectly inherit the object.

Function.prototype.say = function () {//) extends a say method for all functions.

Alert (' Every function can be called '); Functions are derived from the prototype property of the function

}

var func = function () {}; Func.say ();

  

21. Priorities for succession

(var obj = new Object ();  var str = new String ();      var func = new Function (); So the object,string,function are all constructors.)

Inheritance Mode 1:

OBJECT.PROTOTYPE.JC = function (objbase) {

var F = function () {};

F.prototype = objbase;

return new F ();

}

Inheritance Mode 2:

OBJECT.PROTOTYPE.JC = function (objbase) {

This.prototype = objbase;

}

var F = function () {};

F.JC (objbase);

var func = new F ();

Inheritance Mode 3: (Multiple Inheritance)

OBJECT.PROTOTYPE.JC = function () {
var arr = arguments;
for (var i = 0; i < arr.length; i++) {
For (var k in arr[i]) {
THIS.PROTOTYPE[K] = arr[i][k];
}
}
};

var F = function () {};

F.JC ({id:1},{age:2},{say:function () {

Alert (this.id + ":" + this.age);

}})

var fu = new F ();

Fu.say ();

22. Closures

Closures are the provision of variables in high-level scopes to low-level scope access.

var P = function () {

var i = 1;

return function () {

alert (i);

}

};

var p = new P ();    P (); The outermost p can invoke the I inside the P function;

  

23. Functions

The function has 4 identities in JS.

[function]:var func = function () {}; The difference between a function and a method is primarily within a function of what this refers to.

[Method]:var o = {}; O.say = function () {};

[constructor]:var P = function () {}; var o = new P ();

[Apply|call]: Name of function. Apply (object, [parameter array]) function name. Call (object, parameter list);

24.DOM

The DOM operation is the JS operation HTML tag.

Dom Operation:

Create node: Element document.createelement (TagName)

Get node:

According to Id,classname,tagname Element Document.getelementbyxx (xxvalue)

To obtain a child node:

Parent node. childNodes
Parent node. firstchild
Parent node. LastChild

Get sibling nodes:

The current node. nextSibling

To add a node:

Parent node. appendchild ()

Delete a node

Parent node. RemoveChild ()

Set node properties

Dom.setattribute (' property name ', ' property value ')

Dom.getattribute (' attribute name ')

Common Properties: NodeName (uppercase label signature), NodeType (1 label 2 attribute 3 text), NodeValue (text node of text)

Set node text

The element node. InnerText

The text node. nodevalue

Input node. value

DOM Tree: Note that a text node is a separate node.

25.settimeout,setinterval Nature or single-threaded. JS is driven by event mode

  

26.js Compression:

  

Way One:

JavaScript Compressor Online Compression tool

Address: http://dean.edwards.name/packer/

Way two:

JSA compression, obfuscation, analysis tools

:

Way three:

Closure compiler Google's JS optimizer. Very smart.

:

  

[JS] JavaScript basic syntax

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.