Answer the question
In order to leave comments, you need to log in
When and how to declare objects?
1. Object constructor
var person = new Object();
person.name = "Anand",
person.getName = function(){
return this.name ;
};
var person = {
name : "Anand",
getName : function (){
return this.name
}
}
function Person(name){
this.name = name
this.getName = function(){
return this.name
}
}
function Person(){};
Person.prototype.name = "Anand";
function Person(name){
this.name = name;
}
Person.prototype.getName = function(){
return this.name
}
var person = new function(){
this.name = "Anand"
}
Answer the question
In order to leave comments, you need to log in
Vote for unspecified option #7: use classes (one per module) and never shaman anything on prototypes by hand. This will allow you to keep an understanding of what is happening in the code in general for a longer time.
export default class Person {
constructor(name) {
this.name = name;
}
getName() {
return `My name is ${this.name}`;
}
}
// Если нужен singleton, немного меняем экспорт:
//
// const PERSON = new Person();
// export default PERSON;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question