📜  Lodash _.quaternary() 方法

📅  最后修改于: 2022-05-13 01:56:52.148000             🧑  作者: Mango

Lodash _.quaternary() 方法

Lodash _.quaternary() r 返回一个只接受四个参数的新函数并将这些参数传递给给定的函数。其他参数被丢弃。

句法:

_.quaternary( fun )

参数:此方法采用上面列出并在下面讨论的单个参数。

  • fun:这是给定的函数。

返回值:此方法返回一个新函数。

注意:要执行以下示例,您必须使用此命令提示符安装lodash-contrib库并执行以下命令。

npm install lodash-contrib

示例 1:

Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function
function fun(){
    return arguments;
}
  
// Making quaternary function
var gfgFunc = _.quaternary(fun);
  
console.log("Arguments are :", gfgFunc(
    "first", "second", "third", "fourth"));


Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function
function fun(){
    return arguments;
}
  
// Making quaternary function
var gfgFunc = _.quaternary(fun);
  
// Rest arguments are excluded
console.log("Arguments are :", gfgFunc(
    "a", "b", "c", "d", "e", "f"));


Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function
function add(){
    s = 0
    for(i = 0; i < 4; i++){
        s += arguments[i]
    }
    return s;
}
  
// Making quaternary function
var gfgFunc = _.quaternary(add);
  
// Rest arguments are excluded
console.log("Sum of first 4 arguments is :",
    gfgFunc(100, 100, 100, 100, 100));


输出:

Arguments are : [Arguments] {
  '0': 'first',
  '1': 'second',
  '2': 'third',
  '3': 'fourth'
}

示例 2:

Javascript

// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function
function fun(){
    return arguments;
}
  
// Making quaternary function
var gfgFunc = _.quaternary(fun);
  
// Rest arguments are excluded
console.log("Arguments are :", gfgFunc(
    "a", "b", "c", "d", "e", "f"));

输出:

Arguments are : [Arguments] 
  { '0': 'a', '1': 'b', '2': 'c', '3': 'd' }

示例 3:在此示例中,我们将添加参数,但只有前 4 个参数将使用此方法。

Javascript

// Defining lodash contrib variable
var _ = require('lodash-contrib'); 
  
// Function
function add(){
    s = 0
    for(i = 0; i < 4; i++){
        s += arguments[i]
    }
    return s;
}
  
// Making quaternary function
var gfgFunc = _.quaternary(add);
  
// Rest arguments are excluded
console.log("Sum of first 4 arguments is :",
    gfgFunc(100, 100, 100, 100, 100));

输出:

Sum of first 4 arguments is : 400