In the previous article "In-depth analysis of the object copy method in JavaScript (with code)", we learned about the object copy method in JS. The following article will introduce you to some operating methods of Object objects in JS. Let’s take a look.

javascript
Object
Some efficient operation methods
Object.assign()
Method is used to copy the values of all enumerable properties from one or more source objects to the target object. It will return the target object. The
1 2 3 | const object1 = { a: 1, b: 2, c: 3 };
const object2 = Object.assign({ c: 4, d: 5 }, object1);
console.log(object2);
|
Copy after login
Object.create()
method creates a new object, using an existing object to provide the proto
of the newly created object.
1 2 3 4 5 6 7 8 9 10 | const person = {
color: "red" ,
sayName: function () {
console.log(this.name);
},
};
const m = Object.create(person);
m.name = "chuchur" ;
m.sayName();
|
Copy after login
Object.defineProperties()
The method directly defines new properties or modifies existing properties on an object, and returns the object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | var obj = {};
Object.defineProperties(obj, {
property1: {
value: 1,
writable: true,
},
property2: {
value: "Hello" ,
writable: false,
},
});
obj.property1 = 1;
obj.property2 = 2;
console.log(obj);
|
Copy after login
Object.defineProperty()
The method will directly define a new property on an object, or modify an existing property of an object, and return the object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | var o = {};
Object.defineProperty(o, "a" , {
value: 37,
writable: true,
enumerable: true,
configurable: true,
});
var bValue;
Object.defineProperty(o, "b" , {
get: function () {
return bValue;
},
set: function (newValue) {
bValue = newValue;
},
enumerable: true,
configurable: true,
});
o.b = 38;
|
Copy after login
Object.entries()
The method returns an array of key-value pairs for the given object's own enumerable properties, arranged and used for...in
The order returned when looping through the object is the same (the difference is that the for-in
loop also enumerates the properties in the prototype chain).
1 2 3 | const obj = { foo: "bar" , baz: 42 };
console.log(Object.entries(obj));
|
Copy after login
Object.keys()
The method will return an array consisting of the self-enumerable properties of a given object. The order of the property names in the array and the use of for ...in
The order returned when looping through the object is consistent.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | var arr = [ "a" , "b" , "c" ];
console.log(Object.keys(arr));
var obj = { 0: "a" , 1: "b" , 2: "c" };
console.log(Object.keys(obj));
var anObj = { 100: "a" , 2: "b" , 7: "c" };
console.log(Object.keys(anObj));
var myObj = Object.create(
{},
{
getFoo: {
value: function () {
return this.foo;
},
},
}
);
myObj.foo = 1;
console.log(Object.keys(myObj));
|
Copy after login
Object.values()
The method returns an array of all enumerable property values of a given object itself, in the same order as using for...in
The order of the loop is the same (the difference is that the for-in
loop enumerates the properties in the prototype chain).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | var obj = { foo: "bar" , baz: 42 };
console.log(Object.values(obj));
var obj = { 0: "a" , 1: "b" , 2: "c" };
console.log(Object.values(obj));
var an_obj = { 100: "a" , 2: "b" , 7: "c" };
console.log(Object.values(an_obj));
var my_obj = Object.create(
{},
{
getFoo: {
value: function () {
return this.foo;
},
},
}
);
my_obj.foo = "bar" ;
console.log(Object.values(my_obj));
console.log(Object.values( "foo" ));
if (!Object.values)
Object.values = function (obj) {
if (obj !== Object(obj))
throw new TypeError( "Object.values called on a non-object" );
var val = [],
key;
for (key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
val.push(obj[key]);
}
}
return val;
};
|
Copy after login
Object.hasOwnProperty()
The method returns a Boolean value indicating whether the object has the specified property in its own properties.
1 2 3 4 5 6 7 8 9 10 11 12 13 | o = new Object();
o.prop = "exists" ;
function changeO() {
o.newprop = o.prop;
delete o.prop;
}
o.hasOwnProperty( "prop" );
changeO();
o.hasOwnProperty( "prop" );
o.hasOwnProperty( "toString" );
o.hasOwnProperty( "hasOwnProperty" );
|
Copy after login
Object.getOwnPropertyDescriptor()
The method returns the property descriptor corresponding to an own property on the specified object. (Own properties refer to properties that are directly assigned to the object and do not need to be searched from the prototype chain)
1 2 3 4 5 6 7 8 | o = { bar: 42 };
d = Object.getOwnPropertyDescriptor(o, "bar" );
|
Copy after login
Object.getOwnPropertyDescriptors()
The method is used to obtain the properties of an object Descriptors for all self-properties.
The Object.assign() method can only copy the enumerable self-properties of the source object. At the same time, the properties of the properties cannot be copied during copying, and the accessor properties will be converted into data properties. The prototype of the source object cannot be copied. This method can be used in conjunction with the Object.create() method to achieve the above.
1 2 3 4 | Object.create(
Object.getPrototypeOf(obj),
Object.getOwnPropertyDescriptors(obj)
);
|
Copy after login
Object.getOwnPropertyNames()
The method returns a property name of all the own properties of the specified object (including non-enumerable properties but excluding Symbol
value as an attribute of the name).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | var arr = [ "a" , "b" , "c" ];
console.log(Object.getOwnPropertyNames(arr).sort());
var obj = { 0: "a" , 1: "b" , 2: "c" };
console.log(Object.getOwnPropertyNames(obj).sort());
Object.getOwnPropertyNames(obj).forEach( function (val, idx, array ) {
console.log(val + " -> " + obj[val]);
});
var my_obj = Object.create(
{},
{
getFoo: {
value: function () {
return this.foo;
},
enumerable: false,
},
}
);
my_obj.foo = 1;
console.log(Object.getOwnPropertyNames(my_obj).sort());
|
Copy after login
Object.getOwnPropertySymbols()
method returns an array of all Symbol
properties of the given object itself.
1 2 3 4 5 6 7 8 9 10 11 12 | var obj = {};
var a = Symbol( "a" );
var b = Symbol. for ( "b" );
obj[a] = "localSymbol" ;
obj[b] = "globalSymbol" ;
var objectSymbols = Object.getOwnPropertySymbols(obj);
console.log(objectSymbols.length);
console.log(objectSymbols);
console.log(objectSymbols[0]);
|
Copy after login
Object.isPrototypeOf()
The method is used to test whether an object exists on the prototype chain of another object.
1 2 3 4 5 6 7 8 9 10 11 12 13 | function Foo() {}
function Bar() {}
function Baz() {}
Bar.prototype = Object.create(Foo.prototype);
Baz.prototype = Object.create(Bar.prototype);
var baz = new Baz();
console.log(Baz.prototype.isPrototypeOf(baz));
console.log(Bar.prototype.isPrototypeOf(baz));
console.log(Foo.prototype.isPrototypeOf(baz));
console.log(Object.prototype.isPrototypeOf(baz));
|
Copy after login
Object.propertyIsEnumerable()
The method returns a Boolean value indicating whether the specified property is enumerable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | var o = {};
var a = [];
o.prop = "is enumerable" ;
a[0] = "is enumerable" ;
o.propertyIsEnumerable( "prop" );
a.propertyIsEnumerable(0);
var a = [ "is enumerable" ];
a.propertyIsEnumerable(0);
a.propertyIsEnumerable( "length" );
Math.propertyIsEnumerable( "random" );
this.propertyIsEnumerable( "Math" );
var a = [];
a.propertyIsEnumerable( "constructor" );
function firstConstructor() {
this.property = "is not enumerable" ;
}
firstConstructor.prototype.firstMethod = function () {};
function secondConstructor() {
this.method = function method() {
return "is enumerable" ;
};
}
secondConstructor.prototype = new firstConstructor();
secondConstructor.prototype.constructor = secondConstructor;
var o = new secondConstructor();
o.arbitraryProperty = "is enumerable" ;
o.propertyIsEnumerable( "arbitraryProperty" );
o.propertyIsEnumerable( "method" );
o.propertyIsEnumerable( "property" );
o.property = "is enumerable" ;
o.propertyIsEnumerable( "property" );
o.propertyIsEnumerable( "prototype" );
o.propertyIsEnumerable( "constructor" );
o.propertyIsEnumerable( "firstMethod" );
|
Copy after login
Object.getPrototypeOf()
The method returns the <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>const prototype1 = {};
const object1 = Object.create(prototype1);
console.log(Object.getPrototypeOf(object1) === prototype1);
// expected output: true</pre><div class="contentsignin">Copy after login</div></div>
Object.is of the prototype (
internal [[Prototype]]
) property of the specified object. ()
method determines whether two values are the same value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | Object.is( "foo" , "foo" );
Object.is(window, window);
Object.is( "foo" , "bar" );
Object.is([], []);
var test = { a: 1 };
Object.is(test, test);
Object.is(null, null);
Object.is(0, -0);
Object.is(-0, -0);
Object.is(NaN, 0 / 0);
if (!Object.is) {
Object.is = function (x, y) {
if (x === y) {
return x !== 0 || 1 / x === 1 / y;
} else {
return x !== x && y !== y;
}
};
}
|
Copy after login
Object.preventExtensions()
The method makes an object non-extensible, that is, new properties can never be added.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | var obj = {};
var obj2 = Object.preventExtensions(obj);
obj === obj2;
var empty = {};
Object.isExtensible( empty );
Object.preventExtensions( empty );
Object.isExtensible( empty );
var nonExtensible = { removable: true };
Object.preventExtensions(nonExtensible);
Object.defineProperty(nonExtensible, "new" , { value: 8675309 });
function fail() {
"use strict" ;
nonExtensible.newProperty = "FAIL" ;
}
fail();
var fixed = Object.preventExtensions({});
fixed.__proto__ = { oh: "hai" };
|
Copy after login
Object.isExtensible()
The method determines whether an object is extensible (whether new properties can be added to it).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | var empty = {};
Object.isExtensible( empty );
Object.preventExtensions( empty );
Object.isExtensible( empty );
var sealed = Object.seal({});
Object.isExtensible(sealed);
var frozen = Object.freeze({});
Object.isExtensible(frozen);
|
Copy after login
Object.freeze()
The method can freeze an object. Freezing means that new attributes cannot be added to this object, the values of existing attributes cannot be modified, and existing attributes cannot be deleted. Properties, and the enumerability, configurability, and writability of existing properties of the object cannot be modified. This method returns the frozen object.
1 2 3 4 5 6 7 8 9 10 11 | const object1 = {
property1: 42,
};
const object2 = Object.freeze(object1);
object2.property1 = 33;
console.log(object2.property1);
|
Copy after login
Object.isFrozen()
Method to determine whether an object is frozen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | var frozen = { 1: 81 };
Object.isFrozen(frozen);
Object.freeze(frozen);
Object.isFrozen(frozen);
Object.isSealed(frozen);
Object.isExtensible(frozen);
Object.isFrozen({});
var vacuouslyFrozen = Object.preventExtensions({});
Object.isFrozen(vacuouslyFrozen);
var oneProp = { p: 42 };
Object.isFrozen(oneProp);
Object.preventExtensions(oneProp);
Object.isFrozen(oneProp);
delete oneProp.p;
Object.isFrozen(oneProp);
var nonWritable = { e: "plep" };
Object.preventExtensions(nonWritable);
Object.defineProperty(nonWritable, "e" , { writable: false });
Object.isFrozen(nonWritable);
Object.defineProperty(nonWritable, "e" , { configurable: false });
Object.isFrozen(nonWritable);
var nonConfigurable = { release: "the kraken!" };
Object.preventExtensions(nonConfigurable);
Object.defineProperty(nonConfigurable, "release" , { configurable: false });
Object.isFrozen(nonConfigurable);
Object.defineProperty(nonConfigurable, "release" , { writable: false });
Object.isFrozen(nonConfigurable);
var accessor = {
get food() {
return "yum" ;
},
};
Object.preventExtensions(accessor);
Object.isFrozen(accessor);
Object.defineProperty(accessor, "food" , { configurable: false });
Object.isFrozen(accessor);
|
Copy after login
Object.seal()
The method seals an object, preventing new properties from being added and marking all existing properties as non-configurable. The value of the current property can be changed as long as it is writable.
1 2 3 4 5 6 7 8 9 10 11 12 | const object1 = {
property1: 42,
};
Object.seal(object1);
object1.property1 = 33;
console.log(object1.property1);
delete object1.property1;
console.log(object1.property1);
|
Copy after login
Object.isSealed()
Method to determine whether an object is sealed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | var empty = {};
Object.isSealed( empty );
Object.preventExtensions( empty );
Object.isSealed( empty );
var hasProp = { fee: "fie foe fum" };
Object.preventExtensions(hasProp);
Object.isSealed(hasProp);
Object.defineProperty(hasProp, "fee" , { configurable: false });
Object.isSealed(hasProp);
var sealed = {};
Object.seal(sealed);
Object.isSealed(sealed);
Object.isExtensible(sealed);
Object.isFrozen(sealed);
var s2 = Object.seal({ p: 3 });
Object.isFrozen(s2);
var s3 = Object.seal({
get p() {
return 0;
},
});
Object.isFrozen(s3);
|
Copy after login
Object.valueOf()
The method returns the original value of the specified object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | var array = [ "ABC" , true, 12, -5];
console.log( array .valueOf() === array );
var date = new Date (2013, 7, 18, 23, 11, 59, 230);
console.log( date .valueOf());
var num = 15.2654;
console.log(num.valueOf());
var bool = true;
console.log(bool.valueOf() === bool);
var newBool = new Boolean(true);
console.log(newBool.valueOf() == newBool);
console.log(newBool.valueOf() === newBool);
function foo() {}
console.log(foo.valueOf() === foo);
var foo2 = new Function( "x" , "y" , "return x + y;" );
console.log(foo2.valueOf());
var obj = { name: "张三" , age: 18 };
console.log(obj.valueOf() === obj);
var str = "http://www.xyz.com" ;
console.log(str.valueOf() === str);
var str2 = new String( "http://www.xyz.com" );
console.log(str2.valueOf() === str2);
|
Copy after login
Recommended learning: JavaScript video tutorial
The above is the detailed content of An article explaining some operating methods of Object objects in JS (share). For more information, please follow other related articles on the PHP Chinese website!