當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Lodash _.rest()用法及代碼示例

Lodash是一個JavaScript庫,可在underscore.js之上運行。 Lodash幫助處理數組,字符串,對象,數字等。

_.rest()方法用於創建一個函數,該函數使用創建的函數的this綁定以及從起始位置開始的參數數組來調用給定的func。

用法:

_.rest( func, start )

參數:此方法接受上麵提到和下麵描述的兩個參數:

  • func:它是用於向其應用rest參數的函數。
  • start:它是rest參數的開始位置。它是一個可選參數。

返回值:此方法返回新函數。



範例1:

Javascript

// Requiring lodash library 
const _ = require('lodash'); 
  
// Using the _.rest() method 
// with its parameter 
var write = _.rest(function(author, portal) { 
    return author + portal; 
  }, [1]); 
  
// Calling write with its values 
write(['Nidhi', 'GeeksforGeeks']);

輸出:

Nidhi,GeeksforGeeks

範例2:

Javascript

// Requiring lodash library 
const _ = require('lodash'); 
  
// Using the _.rest() method 
// with its parameter 
var called = _.rest(function(who, whom) { 
    return who + ' ' + 
      _.initial(whom).join(', ') + 
      (_.size(whom) > 2 ? ', and ' :'') + 
      _.last(whom); 
  }); 
   
// Calling called with values  
called('Teacher called', 'nidhi', 
       'nisha', 'preeti.');

輸出:

Teacher called nidhi, nisha, and preeti.

相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Lodash _.rest() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。