Lodash _.bound() 方法
Lodash _.bound()方法返回函数,该函数是按名称绑定到所创建对象的对象的属性。
句法:
_.bound( obj, function);
参数:此方法接受上面列出并在下面讨论的两个参数。
- obj:定义函数的对象。
- 函数:定义的包含返回逻辑的函数。
返回值:此方法返回一个函数。
注意:要执行以下示例,您必须使用此命令提示符安装lodash-contrib库并执行以下命令。
npm install lodash-contrib
下面的示例说明了 JavaScript 中的 Lodash _.bound() 方法:
示例 1:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
var gfgObject = {
name: "GeeksforGeeks",
detail: "Computer science portal for geeks",
geekFunc: function() {
return this.name + ": " + this.detail;
}
};
var gfgFunction = _.bound(gfgObject, "geekFunc");
console.log(gfgFunction());
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
var gfgObject = {
one : 1,
two : 2,
geekFunc: function() {
return this.one + " and " + this.two;
}
};
var gfgFunction = _.bound(gfgObject, "geekFunc");
console.log(gfgFunction());
输出:
GeeksforGeeks: Computer science portal for geeks
示例 2:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
var gfgObject = {
one : 1,
two : 2,
geekFunc: function() {
return this.one + " and " + this.two;
}
};
var gfgFunction = _.bound(gfgObject, "geekFunc");
console.log(gfgFunction());
输出:
1 and 2