Part 23 | JavaScript Tutorial | Creating objects in JS | Function Constructor | Object Create method

preview_player
Показать описание
Content:-
An object can be created mainly in 4 ways:
1. Literal way
- Using {} we can create an object.
eg: let a = {name: "Trinits"};

2. Using new keywords.
const person = new Object();
Note:- For readability, simplicity, and execution speed, use the object literal method.

3. Using Function constructor:-
- creating an object using a constructor function. The new keyword is used to create a new instance of the object.
- good practice to name constructor functions with an upper-case first letter.
- Note:- Using the arrow functions, we can't create the Function constructor.

function Person(name, age) {
};
}

const p1 = new Person("Trinits", 20);
const p1 = new Person("Technologies", 25);

- We can add a method to the existing object as given below
};

- We can add the additional properties as given below

- Create an object with an existing object as a prototype

//Creating object based on another one.
const myPrototype = {
prop1: value1,
prop2: value2,
};

5. ES6 Class syntax: This involves defining a class with a constructor and any other methods on the class.

class Person {
constructor(prop1, prop2) {
}
myMethod() {
// method code here
}
}

const myObject = new Person(value1, value2);
Рекомендации по теме
welcome to shbcf.ru