[JS] JavaScript Essentials (1) Basic features

Source: Internet
Author: User

1. Characteristics of global variables:

Variables written directly in the function body (without the VAR flag) are automatically promoted to global variables.

        (function func () {            i = +;        } ());        alert (i);

It is highly recommended not to write Var.

        var num = 1;        (function func () {            i = +;        } ());        Delete I;        Delete num;        if (typeof i! = "undefined") alert (i);        if (typeof num! = "undefined") alert (num);

  

2.DOM operation

A. What is the DOM (Document Object model), which is in the documentation objects models. Includes element nodes, text nodes, attribute nodes, and comment nodes.

B.dom Operation:

Html:write (), Innerhtml,innertext,getelementbyid ()

Css:

    <div>text</div>    <script>        var e = document.getelementsbytagname (' div ') [0];        E.style.background = ' Blue ';    </script>

Event: There are 4 ways to add events.

All Events: Events

3. Exceptions:

try {     throw ' exception happend ';} catch (e) {      

  

4. Type:

5 basic Types in JS: string number Boolean undefined null and a complex type: Object

Attention:

    typeof null is an object.

    Because a null type has only one value null,null that represents a type and a pointer to an empty object.

Because the undefined type has only one value undefined,undefined represents a type and represents an undefined variable.

  

    typeof Object is a function

The Object function string is a constructor. (Recommended after the construction function name Pascal named, Method camel name)

5. Built-in objects

JS has 7 number String Date Array Boolean Math regexp default constructor. The New keyword allows you to define the corresponding object.

Attention:

    Prototype is used to add properties and methods to an object.

constructor is a pointer to the function itself.

6. Events

Event Flow:

Event flow refers to the order in which events are accepted in a page.

Event bubbling refers to the exact opposite of event bubbling from the most specific node to the least specific node.

Event object:

Event objects are generated when a DOM event is triggered.

Type: Event type target: equivalent to Sender Stoppropagation (): Block event bubbling Preventdefault (): Block event default behavior (e.g.: a tag default click Jump event)

To add an event:

AddEventListener (event name, function, whether bubbling)

Attachevent (on Event name, function)

On event = function

A. directly on the HTML

<div onclick= "func ()" >text</div> <script>        function func () {            //todo        } </SCRIPT&G T

B.DOM0 level Processing

    <div >text</div>    <script>        var e = document.getelementsbytagname (' div ') [0];        E.onclick = func;        function func () {            //todo        }    </script>

C.DOM2 level Processing

    <div >text</div>    <script>        var e = document.getelementsbytagname (' div ') [0];        E.addeventlistener (' click ', func);        function func () {            //todo        }    </script>

D.IE8 below

    <div >text</div>    <script>        var e = document.getelementsbytagname (' div ') [0];        A.attachevent (' onclick ', func);        function func () {            //todo        }    </script>

  

7.BOM

BOM (Browserobjectmodel) Browser object model.

Includes: Window,timing,history,location,screen

Window

The Window object represents the browser window. The DOM node belongs to a property of the Window object. All global variables automatically become a property of the Window object.

Timing

    <div>text</div>    <div>text</div>    <script>        setinterval (function () {            var e = getdivs () [0];            e.innerhtml = new Date (). tolocalestring ();        SetTimeout (function () {            var e = Getdivs () [1];            e.innerhtml = new Date (). tolocalestring ();        function Getdivs () {            return document.getelementsbytagname (' div ');        }    </script>

8.DOM Control HTML:

Add: Document.createelement (TagName);

Delete: Element.removechild (Element);

Change: Element.setattribute (Attrname, AttrValue);

Check:

document.getElementById (ElementID);    Element.parentnode; Element.childnodes;

Document.getattribute (Attrname);

9.prototype (prototype)

In addition to the basic types, there are prototype properties.

the attributes in the prototype (including the prototype chain) are directly inherited to the object.

  

10.call and apply

When invoking a method with call and apply, the this in the method can be specified by us.

        function Func (A, b) {            this.sum = a + b;            return this;        }        var O1 = {};        var O2 = {};        Func.call (O1, 3, 5);        Func.apply (O2, [3, 4]);        alert (o1.sum);        alert (o2.sum);

Time:

GMT (Greenwich Mean time)--GMT, world base Time

The letters in UTC are a mixture of English coordinated Universal time and French temp universellecoordinee. World. Atomic time.

[JS] JavaScript Essentials (1) Basic features

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.