查看對象的方法,繼續控制台輸出,如圖: hasOwnProperty():返回一個布爾值,指示對象自身屬性中是否具有指定的屬性(也就是,是否有指定的鍵)。 isPrototypeOf():用於測試一個對象是否存在於另一個對象的原型鏈上。 toString():返回一個表示該對象的字元串。 value ...
查看對象的方法,繼續控制台輸出,如圖:
hasOwnProperty():
返回一個布爾值,指示對象自身屬性中是否具有指定的屬性(也就是,是否有指定的鍵)。
let object1 = new Object(); object1.property1 = 42; object1.hasOwnProperty('property1'); // true object1.hasOwnProperty('toString'); // false
isPrototypeOf():
用於測試一個對象是否存在於另一個對象的原型鏈上。
function Foo() {} function Bar() {} function Baz() {} Bar.prototype = Object.create(Foo.prototype); Baz.prototype = Object.create(Bar.prototype); let baz = new Baz(); Baz.prototype.isPrototypeOf(baz); // true Bar.prototype.isPrototypeOf(baz); // true Foo.prototype.isPrototypeOf(baz); // true Object.prototype.isPrototypeOf(baz); // true
toString()
:返回一個表示該對象的字元串。
let o = new Object(); o.toString(); // '[object Object]'
valueOf():
返回指定對象的原始值。
let o = new Object(); o.valueOf(); // {}
Object.assign():
用於將所有可枚舉屬性的值從一個或多個源對象複製到目標對象。它將返回目標對象。
let target = { a: 1, b: 2 }; let source = { b: 4, c: 5 }; let returnedTarget = Object.assign(target, source); target; // { a: 1, b: 4, c: 5 } returnedTarget; // { a: 1, b: 4, c: 5 }
Object.create():創建一個新對象,使用現有的對象來提供新創建的對象的__proto__。即創建一個以指定的對象為原型的子對象。
Object.setPrototypeOf():設置一個指定的對象的原型 ( 即, 內部[[Prototype]]屬性)到另一個對象或null。
Object.getPrototypeOf():
返回指定對象的原型(內部[[Prototype]]
屬性的值)。
let person = {name: 'people'}; let me = Object.create(person); me.name; // 'people'
let proto = {};
let obj = { x: 10 };
Object.setPrototypeOf(obj, proto);
proto.y = 20;
proto.z = 40;
obj.x // 10
obj.y // 20
obj.z // 40
let p = Object.getPrototypeOf(obj);
proto === p; // true
Object.defineProperties():
直接在一個對象上定義新的屬性或修改現有屬性,並返回該對象。
let obj = {}; Object.defineProperties(obj, { 'property1': { value: true, writable: true }, 'property2': { value: 'Hello', writable: false } }); obj.property1; // true obj.property2; // 'Hello'
Object.defineProperty()
:會直接在一個對象上定義一個新屬性,或者修改一個對象的現有屬性, 並返回這個對象。
let o = {}; let bValue = 1; Object.defineProperty(o, "b", { get : function(){ return bValue; }, set : function(newValue){ bValue = newValue; console.log('bValue變了'); }, enumerable : true, configurable : true }); console.log(o.b); //1 o.b = 2; //'bValue變了'
Object.keys()
:會返回一個給定對象的自身可枚舉屬性組成的數組,數組中屬性名的排列順序和使用for...in迴圈遍歷該對象時返回的順序一致 。
Object.values()
:返回一個給定對象自身的所有可枚舉屬性值的數組,值的順序與使用for...in迴圈的順序相同 ( 區別在於 for-in 迴圈枚舉原型鏈中的屬性 )。
Object.entries()
:返回一個給定對象自身可枚舉屬性的鍵值對數組,其排列與使用for...in迴圈遍歷該對象時返回的順序一致(區別在於 for-in 迴圈還會枚舉原型鏈中的屬性)。
Object.fromEntries()
:把鍵值對列表轉換為一個對象,是Object.entries()
的逆操作。
let obj = { a: 1, b: 2, c: 3 }; let keys = Object.keys(obj); let values = Object.values(obj); let entries = Object.entries(obj); keys; // ['a','b','c'] values; // [1,2,3] entries; // [['a',1],['b',2],['c',3]] let fromEntries = Object.fromEntries(entries); fromEntries; // {a: 1, b: 2, c: 3}
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
先這麼多吧。