- Underscore.JS 教程
- Underscore.JS - 主頁
- Underscore.JS - 概述
- Underscore.JS - 環境設定
- Underscore.JS - 迭代集合
- Underscore.JS - 處理集合
- Underscore.JS - 迭代陣列
- Underscore.JS - 處理陣列
- Underscore.JS - 函式
- Underscore.JS - 對映物件
- Underscore.JS - 更新物件
- Underscore.JS - 比較物件
- Underscore.JS - 工具
- Underscore.JS - 鏈式
- Underscore.JS 有用資源
- Underscore.JS - 快速指南
- Underscore.JS - 有用資源
- Underscore.JS - 討論
Underscore.JS - 屬性方法
語法
_.property(path)
屬性方法返回一個函式,它將返回一個物件的指定屬性。我們也可以傳遞巢狀屬性。請看下面的示例 −
示例
var _ = require('underscore');
var student = {name: 'Sam', age: 10, class : {section : 'B'} }
// Example 1: Get name of student
var getName = _.property('name');
console.log(getName(student));
// Example 2: Get section of student
var getSection = _.property(['class', 'section'])
console.log(getSection(student));
將上面的程式另存為 tester.js。執行以下命令來執行此程式。
命令
\>node tester.js
輸出
Sam B
underscorejs_updating_objects.htm
廣告