为函数自定义bind方法实例页面

HTML代码:
<input id="button" type="button" value="点击我" />
<span id="text">我会变色?</span>
JS代码:
if (!function() {}.bind) {
    Function.prototype.bind = function(context) {
        var self = this
            , args = Array.prototype.slice.call(arguments);
            
        return function() {
            return self.apply(context, args.slice(1));    
        }
    };
}

var eleBtn = document.getElementById("button")
    , eleText = document.getElementById("text");
    
eleBtn.onclick = function(color) {
    color = color || "#003399";
    this.style.color = color;
}.bind(eleText, "#cd0000");

posted on 2015-12-29 14:23  zhangzongshan  阅读(438)  评论(0编辑  收藏  举报

导航