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>

 

posted @ 2017-01-10 18:51  _白马非马  阅读(653)  评论(0编辑  收藏  举报