Publisher : Meck Charles
const person = {
name: "John",
age: 30,
sayHello: function() {
console.log("Hello");
}
};
function Person(name, age) {
this.name = name;
this.age = age;
this.sayHello = function() {
console.log("Hello");
}
}
const person = new Person("John", 30);
Object.create()
Method : You can use the Object.create()
method to create an object with a specified prototype.const personProto = {
sayHello: function() {
console.log("Hello");
}
};
const person = Object.create(personProto);
person.name = "John";
person.age = 30;
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log("Hello");
}
}
const person = new Person("John", 30);
const singleton = (function() {
let instance;
function createInstance() {
const object = new Object("Object instance");
return object;
}
return {
getInstance: function() {
if(!instance) {
instance = createInstance();
}
return instance;
}
};
})();
const instance1 = singleton.getInstance();
const instance2 = singleton.getInstance();
console.log(instance1 === instance2); // true