jquery的 $proxy() 等於 underscore的bind 等於 原生js的 bind 沒毛病吧?

見註釋…………
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jquery事件命名空間</title>
</head>
<body>
<div id="tree"></div>
</body>

<script src="../lib/jquery-1.11.1.js"></script>
<script src="../lib/underscore.js"></script>
<script>
    var Tree = function(element, options) {
        var $tree = this.$tree = $(element);
        //監聽init事件,觸發
        //$tree.on('init', $.proxy(options.onInit, this));//用jquery實現 ok~~~
        //$tree.on('init',options.onInit);//什麼都不寫 則會報錯
       // $tree.on('init',options.onInit.bind(this));//用原生bind實現 ok~~~
        $tree.on('init',_.bind(options.onInit,this));//用 underscore實現
        this.init();
    };

    Tree.prototype.init = function() {
        console.log('tree init!');
        this.$tree.trigger('init');
    };

    var tree = new Tree('#tree', {
        onInit: function() {
            console.log(this.$tree.outerHeight());
        }
    });
</script>
</html>
相關文章
相關標籤/搜索