JavaScript Tips & notation

Source: Internet
Author: User

JavaScript tips:

1 "State machine"

var state = function () {this.count = 0;                This.fun = null;            This.nowcount = 0;                  };                    State.prototype = {Load:function (count,fun) {this.count = count;                    This.fun = fun;                this.nowcount=0;                    }, Trigger:function () {this.nowcount++;                    if (This.nowcount >= this.count) {this.fun ();            }                }            }; //--------------
function Method1 () {S.trigger (); } function Method2 () {S.trigger (); } var s = new State (); S.load (2, function () {Console.log (' execute complete '); }); SetTimeout (method1, 1000); SetTimeout (METHOD2, 1000);

State machines are typically used when multiple tasks are in progress, and the task executes to a certain stage to execute a function!

Scenario: Simultaneously requests multiple asynchronous concurrent executions (Ajax,nodejs io), and processes the result collection, the above is the simplest, but not graceful enough,

This is the best I've ever found. js Asynchronous process article: http://www.docin.com/p-687111809.html~

2 "Special application of SetTimeout  

var hander=settimeout (function () {},100); cleartimeout (hander);

 Scenario 1: Button Three Quick click to trigger event   

     var num = 0;        var hander = 0;        function Btnclick () {            if (hander! = 0) {                cleartimeout (hander);                hander = 0;            }            num++;            if (num >= 3) {                Run ();                num = 0;                Cleartimeout (hander);                hander = 0;            }            hander = SetTimeout (function () {                num = 0;            }, +);        }        function Run () {            console.log (' Run ');        }    <input type= "button" onclick= "Btnclick ()" value= "Quick Click Three-time Trigger"/>

  Scenario 2: Fast multiple clicks trigger only the last time

var hander = 0;        function Btnclick () {            if (hander! = 0) {                cleartimeout (hander);                hander = 0;            }            hander = SetTimeout (function () {                Run ();            },);        }        function Run () {            console.log (' Run ');        }    <input type= "button" onclick= "Btnclick ()" value= "Quick Click Triggers only last"/>

Other ( long-term update ) ....

JavaScript notation:

《. & [] "

     var obj = new Object ();        Obj.add = function (A, b) {            return a + b;        }        Console.log (Obj.add (1, 2));        var obj2 = new Object ();        obj2[' Add ' = function (A, b) {            return a + b;        }        Console.log (Obj2.add (1, 2));

"Prototype" is the most common

     var obj = function (name) {            this.name = name;        }        Obj.prototype.say = function () {            console.log (this.name);        }        Obj.prototype.add = function (A, b) {            return a + N;        }        var o = new obj (' fuck ');        O.say ();        var obj = function (age) {            this.age = age;        };        Obj.prototype = {            Add:function (A, b) {                return this.age;            },            say:function () {                console.log (' @ ') );            }        }        var o = new obj (23333);        Console.log (O.add ());

"Run Create object" simple closure

    var obj2 = function () {            var _name = ' 123 ';            function _add (A, b) {                return a + b+_name;            }            return {                add: _add,                name: _name            };        } ();        Console.log (Obj2.add (1,3));

Object.create Object.defineproperty

        var obj = function () {            this.add = function () {                console.log (' Add ')            }        }        var outo = new obj (); 
   var o = object.create (Outo, {            sub: {                value:function () {                    console.log (' Sub ')}}}        ); C25/>o.add ();        O.sub ();
  var obj = function () {            this.add = function () {                console.log (' Add ')            }        }        var o = new obj ();        Object.defineproperty (o, {            "sub": {                value:function () {                    console.log (' Sub ')                },                writeable: False            }        });        O.sub ();

  __proto__

     var obj = function () {};        Obj.prototype = {            add:function () {                console.log (' Add ');            },            sub:function () {                Console.log (' Sub ');            }        ;        var o = {};//new Object ();        o.__proto__ = Obj.prototype;        O.add ();        O.sub ();
    var o = {};//new Object ();        o.__proto__ = {            add:function () {                console.log (' Add ');            },            sub:function () {                console.log (' Sub ');            }        };        O.__proto__.go = function () {            console.log (' go ');        }        O.add ();        O.sub ();        O.go ();

Call bind

   var obj = new Object ();        Obj.name = ' myname ';        function Add () {            console.log (this.name+ ': Add ');        }        Add.call (obj);        var obj = new Object ();        Obj.name = ' myname ';        function Add () {            console.log (this.name + ': Add ');        }        var newadd = add.bind (obj);        Newadd ();

  

Written at the end:

Prototype "function" built-in properties

__proto__ built-in properties for arbitrary objects

Add.call (obj) executes the add apply under the Obj object domain

Add.bind (obj) binds an obj object execution domain to the Add, and the Obj object does not get Add,bind returns a binding to execute the domain obj function add

Class. Add, which is static and will not be instantiated

Class. Prototype.add, which is a modified prototype, can instantiate the prototype equivalent of this

Object. Add is an add for an object, only the object uses the

Object. __proto__, which is the set of all properties of the object,

Expression: Object. __proto__= class. Prototype is fully inherited for the original property, object. __proto__= another object. __proto__ simple deep copy for objects

Expression: Object. __proto__.add Add or modify add for an object, the same as: Object. __proto__.add () for execution

Same : expression:for (var i in o.__proto__) {Console.log (o.__proto__[i])} to traverse object Properties

Expression: for (var I in class. prototype) {} for traversing class properties

     

JavaScript Tips & notation

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.