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


underscore.js _.noop()用法及代碼示例

Underscore.js是一個JavaScript庫,它使對數組,字符串,對象的操作更加容易和方便。
_.noop()函數用於返回“undefined”,而與傳遞給它的參數無關。

注意:在瀏覽器中使用下劃線函數之前,非常有必要鏈接下劃線CDN。鏈接underscore.js CDN時“_”作為全局變量附加到瀏覽器。

用法:

_.noop();

參數:它采用任何類型的可選參數。

返回值:此函數返回undefined類型的值。



範例1:

<!DOCTYPE html> 
<html> 
  
<head> 
    <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> 
    </script> 
</head> 
  
<body> 
    <script> 
        let str = new String(_.noop()) 
        console.log(`String is ${str}`) 
  
        let obj = new Object(_.noop()) 
        console.log(`Object is ${obj.Object}`) 
  
        let int = _.noop() 
        console.log(`Integer is ${int}`) 
  
        let arr = new Array(_.noop()) 
        console.log(`Array is ${arr[0]}`) 
    </script> 
</body> 
  
</html>

輸出:

範例2:

<!DOCTYPE html> 
<html> 
  
<head> 
    <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> 
    </script> 
</head> 
  
<body> 
    <script> 
        let val = undefined; 
        let val2 = _.noop(); 
  
        console.log(val === val2) 
  
        if (val == val2) 
            console.log( 
                `val and val2 are equal`); 
        else 
            console.log( 
                `val and val2 are not equal`); 
    </script> 
</body> 
  
</html>

輸出:

範例3:將參數傳遞給_.noop()函數。

<!DOCTYPE html> 
<html> 
  
<head> 
    <script src= 
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"> 
    </script> 
</head> 
  
<body> 
    <script> 
        let func = (para1) => { 
            console.log(para1) 
        } 
        console.log("output:") 
        func(_.noop("some value")); 
  
        // Pass function as parameter 
        console.log("output:") 
        console.log(_.noop(func)) 
  
        console.log("output:") 
        console.log(_.noop(func())) 
    </script> 
</body> 
  
</html>

輸出:




相關用法


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