Fifth, $ programming problem

1, write a generic event listener function

 // Event (event) tools, Source: github.com/markyun 
    markyun.Event = { 
       
        // depends on the ability to bind the event were used dom0 || dom2 || IE embodiment 
        @ parameters: the operation element, the name of the event, event handler 
        addEvent: function (Element, type, handler) {
             iF (element.addEventListener) {
                 // event type, the function to be performed, whether capture 
                element.addEventListener (type, handler, to false ); 
            } the else  iF (Element. the attachEvent) { 
                element.attachEvent ( 'ON' + type, function () { 
                    handler.call (Element);  
                });
            } the else {
                element['on' + type] = handler;
            }
        },
        // 移除事件
        removeEvent : function(element, type, handler) {
            if (element.removeEventListener) {
                element.removeEventListener(type, handler, false);
            } else if (element.datachEvent) {
                element.detachEvent('on' + type, handler);
            } else {
                element['on' + type] = null;
            } 
        }
        // stop event (event bubbling mainly because IE does not support event capture) 
        the stopPropagation: function (EV) {
             IF (ev.stopPropagation) { 
                ev.stopPropagation (); 
            } the else { 
                ev.cancelBubble = to true ; 
            } 
        }, 
        // cancel the default behavior of the event 
        preventDefault: function (event) {
             IF (event.preventDefault) { 
                event.preventDefault (); 
            } the else { 
                event.returnValue = false ;
            } 
        } 
        // get the event target 
        getTarget: function (Event) {
             return event.target || event.srcElement; 
        }

2, how to determine whether an object is an array

function isArray(arg) {
    if (typeof arg === 'object') {
        return Object.prototype.toString.call(arg) === '[object Array]';
    }
    return false;
}

3, bubble sort

  • Adjacent each comparison of two numbers, if the former is smaller than the latter, changing the position

var arr = [3, 1, 4, 6, 5, 7, 2];

function bubbleSort(arr) {
for (var i = 0; i < arr.length - 1; i++) {
    for(var j = 0; j < arr.length - 1; j++) {
        if(arr[j + 1] < arr[j]) {
            var temp;
            temp = arr[j];
            arr[j] = arr[j + 1];
            arr[j + 1] = temp;
        }
    }
}
return arr;
}

console.log(bubbleSort(arr));

4, Quick Sort

  • Dichotomy, remove the intermediate numbers, each intermediate number of comparison array, into small left, to the right into the large

var arr = [3, 1, 4, 6, 5, 7, 2];

function quickSort(arr) {
    if(arr.length == 0) {
        return [];    // 返回空数组
    }

    var cIndex = Math.floor(arr.length / 2);
    var c = arr.splice(cIndex, 1);
    var l = [];
    var r = [];

    for (var i = 0; i < arr.length; i++) {
        if(arr[i] < c) {
            l.push(arr[i]);
        } else {
            r.push(arr[i]);
        }
    }

    return quickSort(l).concat(c, quickSort(r));
}

console.log(quickSort(arr));

5, the preparation method of seeking a byte length of a string

  • Assumptions: an English one byte character, a Chinese character occupies two bytes

function GetBytes(str){

        var len = str.length;

        var bytes = len;

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

            if (str.charCodeAt(i) > 255) bytes++;

        }

        return bytes;

    }

alert(GetBytes("你好,as"));

6, bind usage, as well as point functions and how to bind to note

    • bindThe role and calland applythe same, the difference is calland applyimmediately call the function, and bindreturn a function, when you need to call again executed.

      A simple bindfunction to achieve the following

    • Function.prototype.bind = function(ctx) {
          var fn = this;
          return function() {
              fn.apply(ctx, arguments);
          };
      };

 

Guess you like

Origin www.cnblogs.com/Strugglinggirl/p/11058383.html