Underscore.JS - property 方法

语法

_.property(path)

property 方法返回一个函数,该函数将返回对象的指定属性。我们也可以传递嵌套属性。请参见以下示例 −

示例

var _ = require('underscore');

var student = {name: 'Sam', age: 10, class : {section : 'B'} }

// 示例 1:获取学生姓名
var getName = _.property('name');
console.log(getName(student));

// 示例 2:获取学生部分
var getSection = _.property(['class', 'section'])
console.log(getSection(student));

将上述程序保存在tester.js中。运行以下命令执行该程序。

命令

\>node tester.js

输出

Sam
B

underscorejs_updating_objects.html