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


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

Lodash _.before()方法與Lodash _.after()方法相反。此方法用於創建一個函數,該函數使用創建的函數的this綁定和參數來調用func,而調用此函數的次數少於n次。

用法:

_.before(n, func)

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

  • n:此參數包含數字n,該數字定義了不再調用func的調用數。
  • func:此參數保存將要調用的函數。

返回值:此方法返回新的受限函數。

下麵的示例說明了Lodash _.before()方法:



範例1:在此示例中,我們將嘗試調用該函數3次,但僅調用2次。

Javascript

// Requiring the lodash library   
const _ = require("lodash"); 
  
// Using _.before() method 
var gfg = _.before(3, function () { 
    console.log('Saved'); 
}); 
  
// It will print Saved 
gfg();  
  
// It will print Saved 
gfg(); 
  
// It will print nothing 
gfg();

輸出:

Saved
Saved

範例2:

Javascript

// Requiring the lodash library   
const _ = require("lodash");  
  
// Applying _.before() method 
var gfg = _.before(2, function () { 
     console.log('Successful'); 
}); 
  
// It will print Successful 
gfg(); 
  
// It will print nothing 
gfg();

輸出:

Successful

參考:https://docs-lodash.com/v4/before/

相關用法


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