javascript bind

时间:2014-06-01 04:51:27   收藏:0   阅读:481

最近在代码中经常看到bind关键词,之前没用过啊,这里记录下。从微软复制过来的例子:

// Define the original function.
var checkNumericRange = function (value) {
    if (typeof value !== 'number')
        return false;
    else
        return value >= this.minimum && value <= this.maximum;
}

// The range object will become the this value in the callback function.
var range = { minimum: 10, maximum: 20 };

// Bind the checkNumericRange function.
var boundCheckNumericRange = checkNumericRange.bind(range);

// Use the new function to check whether 12 is in the numeric range.
var result = boundCheckNumericRange (12);
document.write(result);

// Output: true

http://msdn.microsoft.com/zh-cn/library/ff841995


bind还有其他用法,感觉使用bind代码会失去可读性啊?

javascript bind,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!