MENU
Object – Static Methods
Copying Copying Copying
Object.assign( target,o1,o2... )
This function returns a clone. Specifically, it copies the String and Symbol properties from one or more source objects to a target object, using the getters in the source and setters in the target. The properties of sources that appear later in the list will overwrite the earlier ones. If the objects have the same properties, the properties of the last object in the list will be chosen.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var o1 = {
a:1,
[Symbol('b')]:2,
c: {cc:3},
get d(){ return 4;}}var o2 = {
a:10,
e:{value:3, enumerable:true},
f:x=>x}var o = Object.assign(
{},o1,o2,'hello',null,100,undefined,Symbol('b'));console.log(o); // Object {0: "h", 1: "e", 2: "l", 3: "l", 4: "o", a: 10, //
c: Object, d: 4, e: Object, f: function…}
</script></body><html>
Note that properties on the prototype chain and non-enumerable properties are not copied.
Object.create( proto[,propertiesObject] )
Returns a new object with the specified prototype object and properties.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var target1 = {}, target2 = {};o1 = Object.assign(target1, {a:1});o2 = Object.create(target2, {b:{value:2}}); // b is a propertyDescriptorconsole.log(o1===target1,o1.a); // true 1console.log(o2===target2,o2.b); // false 2console.log(Object.getOwnPropertyDescriptor(o1, 'a'));// Object {value: 1, writable: true, enumerable: true, // configurable: true}console.log(Object.getOwnPropertyDescriptor(o2, 'b'));// Object {value: 2, writable: false, enumerable: false, // configurable: false}
</script></body><html>
Properties Properties Properties
Object.defineProperty( obj, property, propertyDescriptor )
Adds the named property described by a given descriptor to an object and returns the object. A property descriptor can be either a data descriptor or an accessor descriptor.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var o={},x=5;Object.defineProperty(o,'dd',{ // dataDescriptor
configurable: false, // may be changed or deleted? required. default:false
enumerable: false, // shows up in for...in, Object.keys()...? required.
default:false
writable: false, // can be changed with = ? optional. default: false
value: 100, // the value. optional. default: undefined});Object.defineProperty(o,'ad',{ // accessorDescriptor
configurable: false, // may be changed or deleted? required. default:false
enumerable: false, // shows up in for...in, Object.keys()...? required.
default:false,
get: ()=>x,
set: (p)=>{x+=p;}});console.log(o); // {dd: 100}o.ad=10; console.log(o.ad,x); //15 15
</script></body><html>
Object.defineProperties( obj, propertiesAndDescriptors )
Adds the named properties described by the given descriptors to an object and returns the object.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var o = Object.defineProperties({},{
'a':{value:10},
'b':{value:20},});console.log(o); // {a: 10, b: 20}
</script></body><html>
Object.getOwnPropertyDescriptor( o )
Returns a property descriptor for a named property on an object.
Object.getOwnPropertyDescriptors( o )
Returns an object containing all own property descriptors for an object.
Object.getOwnPropertyNames( o )
Returns an array containing the names of all of the given object's own enumerable and non-enumerable properties.
Object.getOwnPropertySymbols( o )
Returns an array of all symbol properties found directly upon a given object.
Object.getPrototypeOf( o )
Returns the prototype of the specified object.
Object.setPrototypeOf( o )
Sets the prototype (i.e., the internal [[Prototype]] property).
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var o = {a:1,b:2,[Symbol('c')]:3,[Symbol('d')]:4};console.log(Object.getOwnPropertyDescriptor(o,'a'));// Object {value: 1, writable: true, enumerable: true, //
configurable: true}console.log(Object.getOwnPropertyDescriptors(o));// Object {a: Object, b: Object, Symbol(c): Object, //
Symbol(d): Object}console.log(Object.getOwnPropertyNames(o));// ["a", "b"]console.log(Object.getOwnPropertySymbols(o));// [Symbol(c), Symbol(d)]console.log(Object.getPrototypeOf(o));/*Object {
constructor:function Object()
hasOwnProperty:function hasOwnProperty()
isPrototypeOf:function isPrototypeOf()
propertyIsEnumerable:function propertyIsEnumerable()
toLocaleString:function toLocaleString()
toString:function toString()
valueOf:function valueOf()
__defineGetter__:function __defineGetter__()
__defineSetter__:function __defineSetter__()
__lookupGetter__:function __lookupGetter__()
__lookupSetter__:function __lookupSetter__()
get __proto__:function __proto__()
set __proto__:function __proto__()} */
</script></body><html>
To Iterables To Iterables To Iterables
Object.entries( o )
Returns an array of a given object's own enumerable property [key, value] pairs.
Object. fromE ntries( o)
The reverse of Object.entries().
Object.keys( o )
Returns an array containing the names of all of the given object's own enumerable properties.
Object.values( o )
Returns an array of a given object's own enumerable values.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var o = {a:1,b:2,[Symbol('c')]:3,[Symbol('d')]:4};console.log(Object.entries(o)); // [["a",1],["b",2]]console.log(Object.fromEntries(Object.entries(o))); // {a:1, b:2}console.log(Object.keys(o)); // ["a", "b"]console.log(Object.values(o)); // [1, 2]for (v of Object.values(o)) // for…of loop can now be used to iterate over objects!console.log(v); // 1 2
</script></body><html>
Locking Locking Locking
Object.seal( o )
Prevents other code from deleting properties of an object.
Object.preventExtensions( o )
Prevents any extensions of an object.
Object.freeze( o )
Freezes an object: other code can't delete or change any properties.
Object.isSealed( o )
Determines if an object is sealed.
Object.isExtensible( o )
Determines if extending of an object is allowed.
Object.isFrozen( o )
Determines if an object was frozen.
RESETRUNFULL
<!DOCTYPE html><html><body><script>
var o = {a:1};Object.seal(o); delete o.a;
console.log(o.a); // 1Object.preventExtensions(o); o.b=5; console.log(o);//{a:1}Object.freeze(o); o.a=10;
console.log(o.a); // 1console.log(Object.isSealed(o)); // trueconsole.log(Object.isExtensible(o)); // falseconsole.log(Object.isFrozen(o)); // true
</script></body><html>
Equality Equality Equality
Object.is( a,b )
Identical to (a===b), except in two cases:
RESETRUNFULL
<!DOCTYPE html><html><body><script>
console.log(-0===0, // true
Object.is(-0,0)); // falseconsole.log(NaN===NaN, // false
Object.is(NaN,NaN)); // true
</script></body><html>