5.3 values

5.3.1 语法:

_.values(list)

5.3.2 说明:

检索list拥有的可枚举属性值,并返回一个数组(继承属性则不检索)

5.3.3 代码示例:

var result;

// 操作数组
result = _.values(['a', 'b']);
console.log(result) //=> ["a", "b"]

// 操作对象
result = _.values({ one: 1, two: 2, three: 3});
console.log(result) //=> [1, 2, 3]

// 函数
function Stor() {
    this.one = 1;
}
Stor.prototype.two = 2; //通过给Stor对象的prototype属性给Stor对象添加一个two属性
result = _.values(new Stor);
console.log(result) //=> [1]

5.3.4 操作非对象、数组会返回什么呢?

var result;

result = _.values('123');
console.log(result) //=> []

result = _.values(null);
console.log(result) //=> []

关于操作字符串,之前学的方法中大部分都是将字符串转为数组,此方法将不会将字符串转换为数组操作。

5.3.5 不可枚举的属性

var obj = { one: 1, two: 2};
Object.defineProperty(obj, 'three', { value: 3, enumerable: false }); // 此处给obj对象添加一个不可枚举的属性
var result = _.values(obj);
console.log(result) //=> [1, 2]

5.3.6 call方法后的属性会检索吗?

function Stor() {
    this.one = 1;
}
function Bar() {
    Stor.call(this);
    this.two = 2;
}
var result = _.values(new Bar);
console.log(result) //=> {1: "one", 2: "two"}