javascript The Definitive Guide Chapter 22 Advanced Techniques

//22.1 
//22.1.1 safe type detector 
var value = new new the Array (); 
var value = the isArray the instanceof the Array; 
var = typeof the isArray value; // regular expression operators such as return function 
// Since all types is derived from the Object object 
function the isArray (value) { 
    return Object.prototype.toString.call (value) == '[Object the Array]'; 
} 

function isFunction (value) { 
    return Object.prototype.toString.call (value) = = '[Object function]'; 
} 

function isRegExp (value) { 
    return Object.prototype.toString.call (value) == '[Object the RegExp]'; 
} 


// constructor 21.1.2 scoped safety 

function Person ( name, Age, Job) { 
    this.name = name; 
    this.age = Age; 
    this.job = Job; 
}
// properly constructed 
    this.getArea function = () {
var person = new Person ( 'Nicholas ', 29, 'Software Engineer'); // constructor of an object 
// unexpected configured 
person = Person ( 'Nicholas', 29, 'Software Engineer'); // thus function as execution, this point window 

// construct objects safe 
function Polygon (Sides) { 
    IF (the instanceof the this Polygon) { 
        this.sides = Sides; 
        this.getArea = function () { 
            return 0; 
        } 
    } the else { 
        return new new Polygon (Sides ); 
    } 
} 

// inherited problems of theft mode 

function the Rectangle (width, height) { 
    Polygon.call (the this, 2); 
    this.width = width; 
    this.height = height; 
        return this.width * this.height; 
    }; 
}
 
var = new new RECT the Rectangle (. 5, 10); 
Alert (rect.sides); // undefined because the base class Polygon is safe scope 
// when new Rectangle () Constructor already locked the Rectangle = the this 
// by Polygon.call (this, 2); constructor when this = Polygon! 
case Rectangle this Polygon this with different domains and will not be inherited properties // 

// prototypes using inheritance may resolve this problem 
Rectangle.prototype new new Polygon = (0); 


//22.1.3 of lazy loading function 
    // if the page js script will be repeated multiple times 
    // because the browser is that once in the moment will not become determine 
    // So here as long as the first way to determine the request supported 
    // directly covered by the declaration of new functions to the current function. 
    // if statement thereby achieving simplification logic 
function CreateXMLHttpRequest () { 
    if (typeof the XMLHttpRequest! = Undefined) { 
        return function () {
            the XMLHttpRequest new new return (); 
    return function () { 
        } 
    } the else if (! Typeof the ActiveXObject = undefined) {
        function return () { 
          return new new the ActiveXObject ( ''); 
        } 
    } 
} 

//22.1.4 binding function 

var = {Handler 
    Message: 'the Handled the Event', 
    the handleClick: function (Event) { 
        Alert (this.message); 
    } 
}; 

// the callback function (Event) { 
// handler.handleClick (Event); 
//} 

var BTN = document.getElementById ( 'My-BTN'); 

btn.addEventListener ( 'the Click', function (Event) { 
    Handler .handleClick (Event); 
}, to false); 

// Create a closure (the closure of the partition scope) 
function the bind (Fn, context) { 
        return fn.apply (context, arguments); 
    } 
}
 
btn.addEventListener ( 'the Click', the bind (handler.handleClick, Handler), to false); 


//22.1.5 function currying 
// general manner currying 
function curry ( Fn, context) { 
    
    var args = Array.prototype.slice.call (arguments, 2); // table taken from the parameter array (external parameters) after starting 2 
                                                       // 2 before the context parameters Fn 
    return function () { 

        var innerArgs = Array.prototype.slice.call (arguments); // closure function parameter, i.e. the current closure function 
        var finalArgs = args.concat (innerArgs); // external function parameters and the closure function linked     
       
        return fn. Apply (context, finalArgs); 

    }; 
} 

//22.2 tamperproof objects 
// 22.2.1 not expand objects 

Object.preventExtensions (person); // set the object can not be expanded

IF (Object.isExtensible (Person)) { 
 
}

//22.2.2 sealed object 

Object.seal (Person); 
IF (Object.isSealed (Person)) { 

} 

//22.2.3 frozen object 

Object.freeze (person) ; 


//22.3.1 repeating timer 
var interval the = 250; 
the setTimeout (function () { 
    // the processing logic 
    var div = document.getElementById ( 'myDiv'); 
    var left = the parseInt (div.style.left) +5 ; 
    div.style.left = left + "PX"; 
    IF (left <200 is) { 
        the setTimeout (The arguments.callee, interval The); 
    } 
}, interval The); 


//22.3.2 by Yielding Processes 

function the chunk (Array, Process, context) { 
    the setTimeout (function () { 
        var = Array.shift Item (); 
        process.call (context, Item); 

        IF (be array.length> 0) {
            the setTimeout (The arguments.callee, 100); 
        } 
    }, 100); 
} 

var = Data [12,123,1234,453,436]; 

function PRINTVALUE (Item) { 
    var div = document.getElementById ( 'myDiv'); 
    div.innerHTML + = + Item "<br>"; 
} 

the chunk (Data, PRINTVALUE); 


//22.3.3 throttle function 

var = {Processor 
    timeoutId: null, 
    // the actual method of processing a 
    performProcessing: function () { 
        // code actually executed 
    }, 
    Process: function () { 
        the clearTimeout (this.timeoutId); // Clear the current timer, the execution code logic then sets a timer 
        var that = this; // this pointer references 
        }, 100) ; 
    } 
        this .timeoutId = setTimeout (function () {
            that.performProcessing (); 
}; 

// try to begin execution 
processor.process (); 

// simplified mode 
function Throttle (Method, context) { 
    the clearTimeout (method.tId); 
    method.tId = the setTimeout (function () { 
        Method. Call (context); 
    }, 100); 
}; 


//22.4 custom event 


//22.5 drag

  

Guess you like

Origin www.cnblogs.com/ms_senda/p/11525152.html
Recommended