_.bind()函數用於將函數綁定到對象。調用該函數時,其值為對象。
用法:
_.bind(function, object, *arguments)
參數:此函數接受上述和以下描述的三個參數:
- function:此參數保存需要執行的函數。
- object:此參數保存對象元素。
- arguments:此參數需要在元素之間添加一些符號。
返回值:它返回將函數綁定到對象的值。
範例1:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
var fun = function (Geeks) {
return 'Company Name:' + this.Company
+ '\nAddress:' + this.Address
+ '\nContact:' + this.Contact
};
fun = _.bind(fun, {
Company:'GeeksforGeeks',
Address:'Noida',
Contact:'+91 9876543210'
});
console.log(fun());
</script>
</body>
</html>
輸出:
範例2:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
var obj = {
Name:"GeeksforGeeks",
Address:"Noida"
};
var fun = function (Geeks) {
return 'Welcome to ' + this.Name
+ '\nAddress:' + this.Address
};
fun = _.bind(fun, obj);
console.log(fun());
</script>
</body>
</html>
輸出:
相關用法
- AngularJS angular.bind()用法及代碼示例
- JQuery bind()用法及代碼示例
- AngularJS ng-bind用法及代碼示例
- Node.js socket.bind()用法及代碼示例
- Lodash _.bind()用法及代碼示例
- Google AMP amp-bind-recaptcha用法及代碼示例
- Google AMP amp-bind用法及代碼示例
注:本文由純淨天空篩選整理自AshokJaiswal大神的英文原創作品 Underscore.js | _.bind() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。