1蹋凝、Object.prototype.toString
數(shù)組柠硕、對象判斷具體的類型
Object.prototype.toString.call(abc) //[object Object]
Object.prototype.toString.call(item) //[object Array]
2工禾、Object.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);
// expected output: Object { a: 1, b: 4, c: 5 }
3闻葵、Object.defineProperties()
var obj = {};
Object.defineProperties(obj, {
'property1': {
value: true,
writable: true
},
'property2': {
value: 'Hello',
writable: false
}
// etc. etc.
});
obj:{property1: true, property2: "Hello"}
4民泵、Object.defineProperty()
const object1 = {};
Object.defineProperty(object1, 'property1', {
value: 42,
writable: false
});
object1.property1 = 77;
// throws an error in strict mode
console.log(object1.property1);
// expected output: 42
[http://www.reibang.com/p/8fe1382ba135]
5、Object.fromEntries()
Object.fromEntries() 方法把鍵值對列表轉(zhuǎn)換為一個對象槽畔。
// Map 轉(zhuǎn)化為 Object
const entries = new Map([
['foo', 'bar'],
['baz', 42]
]);
const obj = Object.fromEntries(entries);
console.log(obj);
// expected output: Object { foo: "bar", baz: 42 }
// Array 轉(zhuǎn)化為 Object
const arr = [ ['0', 'a'], ['1', 'b'], ['2', 'c'] ];
const obj = Object.fromEntries(arr);
console.log(obj); // { 0: "a", 1: "b", 2: "c" }
6栈妆、Object.is()
**Object.is()**
方法判斷兩個值是否為同一個值。
Object.is('foo', 'foo'); // true
Object.is(window, window); // true
Object.is('foo', 'bar'); // false
Object.is([], []); // false
var foo = { a: 1 };
var bar = { a: 1 };
Object.is(foo, foo); // true
Object.is(foo, bar); // false
Object.is(null, null); // true
// 特例
Object.is(0, -0); // false
Object.is(0, +0); // true
Object.is(-0, -0); // true
Object.is(NaN, 0/0); // true