_.defaults()函數使用以下默認對象列表中的第一個值填充其未定義的屬性後,返回該對象。
用法:
_.defaults(object, *defaults)
參數:該函數接受上麵提到並在下麵描述的兩個參數:
- object:此參數保存對象的值。
- defaults:它是一個可選參數。它包含一個對象的[鍵,值]對。
返回值:在使用以下默認對象列表中的第一個值填充其未定義的屬性後,它將返回該對象。
範例1:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
var info = {
Company:'GeeksforGeeks',
Address:'Noida',
Contact:'+91 9876543210'
};
console.log(_.defaults(info,
{
Contact:'+91 9898989898',
Name:'Rakesh'
})
);
</script>
</body>
</html>
輸出:
範例2:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src=
"https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js">
</script>
</head>
<body>
<script type="text/javascript">
var info = {
Company:'GeeksforGeeks',
Address:'Noida',
Contact:'+91 9876543210'
};
var def = {
Name:'Ashok',
Age:'34',
Company:'GFG'
}
console.log(_.defaults(info, def));
</script>
</body>
</html>
輸出:
相關用法
- Lodash _.defaults()用法及代碼示例
- PHP imagecreatetruecolor()用法及代碼示例
- p5.js year()用法及代碼示例
- d3.js d3.utcTuesdays()用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- PHP array_udiff_uassoc()用法及代碼示例
- PHP geoip_continent_code_by_name()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP GmagickPixel setcolor()用法及代碼示例
- PHP opendir()用法及代碼示例
- PHP cal_to_jd()用法及代碼示例
- d3.js d3.bisectLeft()用法及代碼示例
- PHP stream_get_transports()用法及代碼示例
注:本文由純淨天空篩選整理自AshokJaiswal大神的英文原創作品 Underscore.js | _.defaults() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。