Underscore.js _.bound() 方法
_.bound()方法返回函数,它是按名称绑定到对象的属性的对象。
句法:
_.bound( obj, function);
参数:
- obj:定义函数的对象。
- 函数:定义的包含返回逻辑的函数。
返回值:此方法返回一个函数。
注意:这在普通 JavaScript 中不起作用,因为它需要安装 underscore.js contrib 库。 Underscore.js contrib 库可以使用npm install underscore-contrib –save 安装。
示例 1:
Javascript
// Defining underscore contrib variable
var _ = require('underscore-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 underscore contrib variable
var _ = require('underscore-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 underscore contrib variable
var _ = require('underscore-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