Lodash _.bind()方法用於創建一個函數,該函數將使用thisArg的this綁定調用給定函數,並用於將函數綁定到對象。調用該函數時,其值為對象。 _.bind.placeholder值(在整體構建中默認為_)可用作部分應用的參數的占位符。
用法:
_.bind(func, thisArg, partials)
參數:此方法接受上述和以下所述的三個參數:
- func:此參數保存將要綁定的函數。
- thisArg:此參數保存對象元素。
- partials:此參數需要在元素之間添加一些符號。
返回值:此方法返回一個新的綁定函數。
下麵的示例說明了Lodash _.bind()方法:
範例1:
Javascript
// Acquiring lodash variable
const _ = require('lodash');
// Function
var fun = function(Geeks) {
return 'Company Name:' + this.Company
+ '\nAddress:' + this.Address
+ '\nContact:' + this.Contact
};
// Use of bind() function
var func = _.bind(fun, {
Company:'GeeksforGeeks',
Address:'Noida',
Contact:'+91 9876543210'
});
console.log(func());
輸出:
Company Name:GeeksforGeeks Address:Noida Contact:+91 9876543210
範例2:
Javascript
// Lodash variable
const _ = require('lodash');
var obj = {
Name:"GeeksforGeeks",
Address:"Noida"
};
var fun = function (Geeks) {
return 'Welcome to ' + this.Name
+ '\nAddress:' + this.Address
};
var func = _.bind(fun, obj);
console.log(func());
輸出:
Welcome to GeeksforGeeks Address:Noida
相關用法
- Node.js socket.bind()用法及代碼示例
- ReactJS bind()用法及代碼示例
- JQuery bind()用法及代碼示例
- AngularJS ng-bind用法及代碼示例
- AngularJS angular.bind()用法及代碼示例
- Underscore.js _.bind()用法及代碼示例
- Google AMP amp-bind-recaptcha用法及代碼示例
- Google AMP amp-bind用法及代碼示例
- Lodash _.method()用法及代碼示例
- Lodash _.sneq()用法及代碼示例
注:本文由純淨天空篩選整理自skyridetim大神的英文原創作品 Lodash _.bind() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。