JAVASCRIPT Tutorial
const object = { property1: value1, property2: value2, ... };
new
keyword is used to call a constructor function and create an object.function ConstructorFunction() {
// Object properties and methods
}
const object = new ConstructorFunction();
class ClassName {
constructor() {
// Object properties and methods
}
}
const object = new ClassName();
new
Keywordnew
keyword is an operator that creates a new object and sets its properties and methods.this
keyword to the newly created object.const object = new ConstructorFunction();
// Object Literal
const person = { name: "John", age: 30 };
// Constructor Function
function Person(name, age) {
this.name = name;
this.age = age;
}
const person2 = new Person("Jane", 25);
// Class
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
const person3 = new Person("Bob", 40);