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


Underscore.js _.propertyOf()用法及代碼示例

_.propertyOf()函數是_.property()函數的反函數。該函數將一個對象作為參數,並返回一個函數,該函數將返回提供的屬性的值。

用法:

_.propertyOf( object )

參數:該函數接受上麵提到並在下麵描述的一個參數:

  • object:此參數保存需要返回函數的對象的值。

返回值:它返回對象的屬性。

範例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(_.propertyOf(info)('Company')); 
    </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:{ name:'GeeksforGeeks' }, 
            Contact:{ Address:  
                { AddressInfo:'Noida', ContNo:'+91 9876543210' } } 
        }; 
  
        console.log(_.propertyOf(info)('Company', 'name')); 
        console.log(_.propertyOf(info)('Contact', 'Address', 'AddressInfo')); 
    </script> 
</body> 
  
</html>

輸出:




相關用法


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