- 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
广告