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
廣告
© . All rights reserved.