object方法

46 阅读1分钟

keys

会返回一个由一个给定对象的自身可枚举属性组成的数组,数组中属性名的排列顺序和正常循环遍历该对象时返回的顺序一致

var obj = {0:'熊大'1'熊二',2:'光头强'}
var keyValue = Object.keys(obj)
console.log(keyValue)

values

返回一个给定对象自身的所有可枚举属性值的数组,值的顺序与使用for..in循环的顺序相同( 区别在于 for-in 循环枚举原型链中的属性)。

var obj = [ foo: "bar , baz: 42 ;
登录后复制
console.log(Object.values(obj)); // [bar "42]
6
0
// array Like object
var obj = { e: 'a', 1: 'b', 2: 'c'];console.log(object.values(obj)); // [a,"b,'c']var an obj = [ 100: "a', 2: "b', 7: c' ];console.log(object.values(an obj)); // [b,'c, a']
var my obj = Object.create([}, [ getFoo: [ value: function() [ return this.foo; ) my obj.foo = 'bar'.console.log(object.values(my obj)); // [bar']
// non-object argument will be coerced to an objectconsole.log(0bject.values('foo')); // [f,o"o"]

defineProperties 返回一个给定对象自身可枚举属性的键值对数组,其排列与使用 for...in 循环遍历该对象时返回的顺序一致 (区别在于 for-in 循环还会枚举原型链中的属性)。

object1 = {
a:'somestring'
b: 42};
Object.entries(object1)// [Array(2),Array(2)]//0: (2) ["a","somestring"]//1: (2) ["b"42]
for (const [key, value] of object.entries(object1)) {console.log(~${key}: ${value]');

create 创建一个新对象,使用现有的对象来提供新创建的对象的_proto_.

const obj = { name: "nordon", }; const newObj = Object.create(obj, { name: { value: "wy", writable: true, configurable: true, enumerable: true }, age: { value: 12, writable: true, configurable: true, enumerable: true }, });

assign 将一个或者多个源对象中所有可枚举的自有属性复制到目标对象,并返回修改后的目标对象。

const target = { a: 1, b: 2 };
const source = { b: 4, c: 5 };

const returnedTarget = Object.assign(target, source);

console.log(target);
// Expected output: Object { a: 1, b: 4, c: 5 }

console.log(returnedTarget === target);
// Expected output: true