24 JavaScript Object Methods That You Should Know

24 JavaScript Object Methods That You Should Know

Hey! JavaScript Developers, In this post we will discuss javascript object methods that you must know to work with object-oriented programming in JavaScript. Objects are vital parts of every programming language once you know how to work with them you can build projects that solve real-life problems.

In this post, we will discuss basic concepts of JS Object like how to create an object in javascript, add & access properties of an object, how to freeze and seal object, etc.

Also Read: JavaScript Array Methods That You Should Know

Apart from these basic concepts, we will walk through advanced concepts like Prototypal Inheritance, Class Inheritance, Encapsulation, Polymorphism, Factory Pattern, singleton pattern, etc

So let’s dive into JavaScript Object!

24 JavaScript Object Methods That You Should Know

Creating An Objects:1) Object Literal
2) using New Object()
3) Using Constructor Function
4) Using Object.create()
5) Using ES6 Classes
Accessing The Properties of Object: 1) Accessing Properties using Dot Notation
2) Accessing Properties using Bracket Notation
JavaScript Object Methods: 1) Object. assign()
2) Object.defineProperty()
3) Object.defineProperties()
4) Object.freeze()
5) Object.seal()
6) object.is()
7) Object.create()
JavaScript Object Methods For Getting Properties & Keys: 1) Object.keys()
2) Object.values()
3) Object.entries()
4) Object.getIwnPropertyNames()
5) Object.getOwnPropertyDescriptor()
6) Object.hasOwn()
Js Prototypes & Inheritance: 1) Object.getPrototypeOf()
2) Object.setPrototypeof()
Javascript Object Methods (other useful): 1) Object.isExtensible()
2) Object.preventExtensions()

Also Read: How To Make CRUD API’s With MongoDB With Express

3 Ways Of Creating An Object In JavaScript:

let person = {
name: "Hemant",
age: "22"
}
let person = new Object();
person.name = "John";
person.age = 30;
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
}
let person = new Person("John", 30);

Accessing The Properties Of Object

person.name = "John";
console.log(person.name); // "John"
person["name"] = "John";
console.log(person["name"]); // "John"

JavaScript Methods To Manipulate Object

let target = { a: 1 };
let source = { b: 2, c: 3 };
Object.assign(target, source);
console.log(target); // { a: 1, b: 2, c: 3 }
let obj = { prop: 1 };
Object.freeze(obj);
obj.prop = 2; // No effect
console.log(obj.prop); // 1
let obj = { prop: 1 };
Object.seal(obj);
obj.newProp = 2; // No effect
console.log(obj.newProp); // undefined
console.log(Object.is(42, 42)); // true
console.log(Object.is(0, 0)); // true
console.log(Object.is(0, -0)); // false
let newObject = Object.create(person);
console.log(newObject.name); // "John"

Js Objects For Getting Object Properties & keys

console.log(Object.keys(person)); // ["name", "age"]
console.log(Object.values(person)); // ["John", 30]
console.log(Object.entries(person)); // [["name", "John"], ["age", 30]]
console.log(Object.getOwnPropertyNames(person)); // ["name", "age"]
console.log(Object.hasOwn(person, "name")); // true

Js Other Useful Methods:

let obj = {};
console.log(Object.isExtensible(obj)); // true

let obj = {};
Object.preventExtensions(obj);
obj.newProp = “test”; // No effect
console.log(obj.newProp); // undefined

Some Advanced Object Features

  1. Property Attributes Like ( writable, enumerable, configurable )
  2. Object Cloning
  3. JSON.stringify
  4. JSON.parse()
let obj = {};
Object.defineProperty(obj, 'name', {
  value: 'Alice',
  writable: false,
  enumerable: true,
  configurable: true
});
console.log(obj.name); // "Alice"
obj.name = 'Bob'; // No effect because writable is false
let original = { name: 'Alice' };
let clone = Object.assign({}, original);
console.log(clone); // { name: 'Alice' }
let obj = { name: 'Alice', age: 25 };
let jsonString = JSON.stringify(obj);
console.log(jsonString); // '{"name":"Alice","age":25}'
let jsonString = '{"name":"Alice","age":25}';
let obj = JSON.parse(jsonString);
console.log(obj); // { name: 'Alice', age: 25 }

Conclusion On JavaScript Object Method

Understanding all the above JavaScript Object Methods Helps You To Be a Better programmer and create software that is efficient in solving the real-world problems. Js Objects are fundamental to structuring and manipulating data. The Key Takeaways of these posts are as follows:

  1. Efficiency In Data handling
  2. Dynamic & Flexible Code
  3. Enhanced Data Integrity
  4. Improved Debugging & Maintenance.

Last Updated: August 11, 2024

By JSM Hemant

About Author

Hello, Myself Hemant. I Am Web Developer & Likes To Create Awesome Projects By Using MERN, Java, Python, C++. In This Website You Will Get Cool Projects Made With Above Technologies With Full Source Code. You Can Also Follow This Website On Author Social Media:

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

Recent Posts