Anything that has a state and behavior can be termed an Object, be it physical or logical. An Object is an entity mentioned in the OOPs concept and is frequently found in JavaScript codes.
Creating an object in JavaScript is a direct phenomenon as JavaScript is a template-based language and is not based on classes.
Ways of Creating Objects in JavaScript
Using an object literal:
Syntax:
Object ={property1 : value1, property2 : value2..... propertyN : valueN}
Using an instance of Object
Syntax:
var objectname = new Object();
Accessing Object Properties
Syntax 1:
objectName.propertyName
Syntax 2:
objectName["propertyName"]
Accessing Object Methods
Syntax:
objectName.methodName()
Example:
<!DOCTYPE html> <html> <body> <p id="example"></p> <script> var student = { first_Name: "Rupali", last_Name : "Sharma", id : 789045 }; document.getElementById("example").innerHTML = student.id + " " + student.first_Name + " " + student.last_Name; </script> </body> </html>
JavaScript Object Methods:
assign() Method:
Use: To copy enumerable and own properties from a source object to a target object.
create() Method:
Use: To create a new object with the specified prototype object and properties.
defineProperty() Method:
Use: To describe some behavioral attributes of the property.
defineProperties() Method:
Use: To create or configure multiple object properties.
entries() Method:
Use: To get an array with arrays of the key, and value pairs.
freeze() Method:
Use: To prevent existing properties from being removed.
getOwnPropertyDescriptor() Method:
Use: To get a property descriptor for the specified property of the specified object.
getOwnPropertyDescriptors() Method:
Use: To get all own property descriptors of a given object.
getOwnPropertyNames() Method:
Use: To get an array of all properties found.
getOwnPropertySymbols() Method:
Use: To get an array of all own symbol key properties.
getPrototypeOf() Method:
Use: To get the prototype of the specified object.
is() Method:
This method determines whether two values are the same value.
preventExtensions() Method:
Use: To prevent any extensions of an object.
seal() Method:
Use: To prevent new properties from being added and mark all existing properties as non-configurable.
setPrototypeOf() Method:
Use: To set the prototype of a specified object to another object.
values() Method:
Use: To get an array of values.