JavaScript Revision (Part-2): Advanced Object Concepts

JavaScript Revision (Part-2): Advanced Object Concepts

In this part, we dive deeper into JavaScript objects and explore advanced concepts that will help to write more efficient and maintainable code. Let's get started!

JavaScript Revision (Part-2): Advanced Object Concepts

In this part, we dive deeper into JavaScript objects and explore advanced concepts that will help to write more efficient and maintainable code. Let's get started!

Singleton Objects:

  • A singleton object ensures that a class has only one instance and provides a global point of access to it.

const singleton = (function() { let instance; function createInstance() { return { name: "SingletonInstance" }; } return { getInstance: function() { if (!instance) { instance = createInstance(); } return instance; } }; })();        

Object Literals:

  • Simple syntax for creating objects using curly braces.

const person = { name: "Sameer", age: 23 };        

Methods of Accessing Objects:

  • Dot notation and bracket notation.

console.log(person.name); // Dot notation
console.log(person["age"]); // Bracket notation
        

Symbols:

- Unique and immutable data type used to create unique property keys.

  const sym = Symbol("uniqueKey");
const obj = {
      [sym]: "value"
  };        

Preventing Object Modification:

- Object.freeze(): Prevents modification of existing properties and prevents new properties from being added.

  const obj = { name: "Alice" };
  Object.freeze(obj);
  obj.name = "Bob"; // Will not change        

Function Calls by Reference and Value:

- Passing objects by reference allows modification of the original object.

  function modify(obj) {
    obj.name = "Bob";
  }
  const person = { name: "Alice" };
  modify(person); // person.name is now "Bob"        

Using this Keyword:

- Refers to the object from which the method was called.

  const person = {
 name: "sameer",

      greet: function() {

          console.log(`Hello, my name is ${this.name}`);

      }

  };

  person.greet(); // Hello, my name is sameer        

Combining Objects:

- Object.assign() and spread operator (`...`) to merge objects.

  const obj1 = { a: 1, b: 2 };
const obj2 = { b: 3, c: 4 };
  const merged = Object.assign({}, obj1, obj2); // { a: 1, b: 3, c: 4 }
  const spreadMerged = { ...obj1, ...obj2 }; // { a: 1, b: 3, c: 4 }        

Using Object.entries, Object.keys, and Object.values:

- Object.entries(): Returns an array of a given object's key-value pairs.

  const entries = Object.entries(person); // [["name", "Alice"], ["age", 30]]        

- Object.keys(): Returns an array of a given object's property names.

  const keys = Object.keys(person); // ["name", "age"]        

- Object.values(): Returns an array of a given object's values

  const values = Object.values(person); // ["Alice", 30]        

Checking if a Key Exists:

- hasOwnProperty(): Checks if the object has the specified property as its own property.

  console.log(person.hasOwnProperty("name")); // true
  console.log(person.hasOwnProperty("gender")); // false        

Connect with me for more articles, tips, and discussions on web development and JavaScript!


#JavaScript #WebDevelopment #Coding #Programming #Tech #LearnToCode #Developer #JavaScriptTutorial

要查看或添加评论,请登录

Sameer Dixit的更多文章

社区洞察

其他会员也浏览了