DEV Community

Madhuri Padegal
Madhuri Padegal

Posted on

JS OBJECTS

Booleans can be objects (if defined with the new keyword)
Numbers can be objects (if defined with the new keyword)
Strings can be objects (if defined with the new keyword)
Dates are always objects
Math are always objects
Regular expressions are always objects
Arrays are always objects
Functions are always objects
Objects are always objects

OBJECT properties:
Properties are the values associated with a JavaScript object.
A JavaScript object is a collection of unordered properties.
Properties can usually be changed, added, and deleted, but some are read only.
Accessing JS objects:
objectName.property or ObjectName['property']
JS Objects methods:
For example we have
var person = {
 first_Name: "John",
 last_Name : "Doe",
 id : 5566,
full_name : function() {
 return this.first_Name + " " + this.last_Name;
 }
};
In a function definition, this refers to the "owner" of the function.
In the example above, this is the person object that "owns" the full_Name function.
In other words, this.first_Name means the firstName property of this object.

Accessing Object Methods:
ObjectName followed by . and methodName. In the above example person.full_name().
Object constructors:
function Person(first, last, age, eye) {
 this.first_Name = first;
 this.last_Name = last;
 this.age = age;
 this.eye_Color = eye;
}
//create the person objects
var my_Father = new Person("John", "Doe", 50, "blue");
Object Methods:

-->Object.assign()
Copies the values of all enumerable own properties from one or more source objects to a target object.
-->Object.create()
Creates a new object with the specified prototype object and properties.
-->Object.defineProperty()
Adds the named property described by a given descriptor to an object.
-->Object.defineProperties()
Adds the named properties described by the given descriptors to an object.
-->Object.entries()
Returns an array containing all of the [key, value] pairs of a given object's own enumerable string properties.
-->Object.freeze()
Freezes an object. Other code cannot delete or change its properties. 
-->Object.fromEntries()
Returns a new object from an iterable of [key, value] pairs. (This is the reverse of Object.entries).
-->Object.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.isExtensible()
Determines if extending of an object is allowed.
-->Object.isFrozen()
Determines if an object was frozen.
-->Object.isSealed()
Determines if an object is sealed.
-->Object.keys()
Returns an array containing the names of all of the given object's own enumerable string properties.
-->Object.preventExtensions()
Prevents any extensions of an object.
-->Object.seal()
Prevents other code from deleting properties of an object.s.

Top comments (0)