Javascript Object - 21 Methods (Reference)
In Javascript Object is a entity that can hold properties and methods. Basically, it is a collection of key-value pairs.
Contents:
defineProperty()
Adds the named property described by a given descriptor to an object.
// Object.defineProperty(obj, prop, descriptor);
var obj = {};
Object.defineProperty(obj, '111', {
value: "aaa",
writable: false,
enumerable: false,
configurable: false
});
obj.111;
// aaa
defineProperties()
Adds the named properties described by the given descriptors to an object.
// Object.defineProperties(obj, props)
var obj = {};
Object.defineProperty(obj, {
111 : {
value: "aaa",
writable: false,
enumerable: false,
configurable: false
},
222 : {
value: "bbb",
writable: false,
enumerable: false,
configurable: false
}
});
obj.111;
// aaa
obj.222;
// bbb
getOwnPropertyDescriptor()
It returns a property descriptor for a named property on an object.
// Object.getOwnPropertyDescriptor(obj, prop)
var obj = {
111: "aaa"
};
var x = Object.getOwnPropertyDescriptor(obj, "111");
// x = {
// value: "aaa",
// writable: true,
// enumerable: true,
// configurable: true
// }
getOwnPropertyDescriptors()
Returns an object containing all own property descriptors for an object.
// Object.getOwnPropertyDescriptors(obj)
var obj = {
111: "aaa",
222: "bbb"
};
var x = Object.getOwnPropertyDescriptors(object1);
// x = {
// 111 : {
// value: "aaa",
// writable: true,
// enumerable: true,
// configurable: true
// },
// 222: {
// value: "bbb",
// writable: true,
// enumerable: true,
// configurable: true
// }
getOwnPropertyNames()
Returns an array containing the names of all of the given object's own enumerable and non-enumerable properties.
// Object.getOwnPropertyNames(obj)
var obj = {
111: "aaa",
222: "bbb"
};
var x = Object.getOwnPropertyNames(obj);
// x = ["111", "222"]
getOwnPropertySymbols()
Returns an array of all symbol properties found directly upon a given object.
// Object.getOwnPropertySymbols(obj)
var obj = {};
obj[Symbol("a")] = "localSymbol";
obj[Symbol.for("b")] = "globalSymbol";
var x = Object.getOwnPropertySymbols(obj);
// x = [Symbol(a), Symbol(b)]
keys()
Returns an array containing the names of all of the given object's own enumerable string properties.
// Object.keys(obj)
var obj = {
111: "aaa",
222: "bbb"
};
var x = Object.keys(obj);
// x = ["111", "222"]
values()
Returns an array containing the values that correspond to all of a given object's own enumerable string properties.
// Object.values(obj)
var obj = {
111: "aaa",
222: "bbb"
};
var x = Object.values(obj);
// x = ["aaa", "bbb"]
getPrototypeOf()
Returns the prototype (internal [[Prototype]] property) of the specified object.
// Object.getPrototypeOf(obj)
var obj = {
111: "aaa"
};
var x = Object.create(obj);
// x = { 111: "aaa" }
setPrototypeOf()
Sets the object's prototype (its internal [[Prototype]] property).
// Object.setPrototypeOf(obj, prototype)
const obj1 = {
111: "aaa"
};
const obj2 = {
111: "bbb"
};
Object.setPrototypeOf(obj1, obj2);
var x = Object.getPrototypeOf(obj1);
// x = { 111: "bbb" }
isExtensible()
Determines if extending of an object is allowed.
// Object.isExtensible(obj)
var obj = {
111: "aaa"
};
var x = Object.isExtensible(obj);
// x = true
preventExtensions()
Prevents any extensions of an object.
// Object.preventExtensions(obj)
var obj = {
111: "aaa"
};
Object.preventExtensions(obj);
var x = Object.isExtensible(obj);
// x = false
seal()
Prevents other code from deleting properties of an object.
// Object.seal(obj)
var obj = {
111: "aaa"
};
Object.seal(obj);
// delete object1.111 cannot be done
isSealed()
Determines if an object is sealed.
// Object.isSealed(obj)
var obj = {
111: "aaa"
};
var x = Object.isSealed(obj);
// x = false
freeze()
Freezes an object. Other code cannot delete or change its properties.
// Object.freeze(obj)
const obj = {
111: "aaa"
};
Object.freeze(obj);
// delete object1.111 cannot be done
// object1.222 = "bbb" cannot be done
isFrozen()
Determines if an object was frozen.
// Object.isFrozen(obj)
var obj = {
111: "aaa"
};
var x = Object.isFrozen(obj);
// x = false
is()
Compares if two values are the same value. Equates all NaN values (which differs from both Abstract Equality Comparison and Strict Equality Comparison).
// Object.is(value1, value2);
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
// Special Cases
Object.is(0, -0); // false
Object.is(-0, -0); // true
Object.is(NaN, 0 / 0); // true
assign()
Copies the values of all enumerable own properties from one or more source objects to a target object.
// Object.assign(target, source);
var obj1 = {
111 : "aaa",
222 : "bbb"
};
var obj2 = {
222 : "BBB",
333 : "ccc"
};
Object.assign(obj1, obj2);
// obj1 = {
111 : "aaa",
222 : "BBB",
333 : "ccc"
}
create()
Creates a new object with the specified prototype object and properties.
// Object.create(proto, [propertiesObject]);
var obj1 = {
111 : "aaa",
222 : "bbb"
};
var x = Object.create(obj1);
x.333 = "ccc";
// x = {
// 333 : "ccc"
// }
x.333;
// ccc
x.__proto__.111;
// aaa
x.111;
// aaa
entries()
Returns an array containing all of the [key, value] pairs of a given object's own enumerable string properties.
// Object.entries(obj)
var obj = {
111: "aaa",
222: "bbb"
};
var x = Object.entries(obj);
// x = [["111", "aaa"], ["222", "bbb"]]
fromEntries()
Returns a new object from an iterable of [key, value] pairs.
// Object.fromEntries(iterable);
var arr = [
["111", "aaa"],
["222", "bbb"]
];
var x = Object.fromEntries(arr);
// x = {
// 111: "aaa",
// 222: "bbb"
// }
