查看物件的方法,繼續控制臺輸出,如圖:

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
先這么多吧,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/158300.html
標籤:JavaScript
上一篇:JS面向物件
下一篇:JS實作簡易計算器的7種方法
