Answer the question
In order to leave comments, you need to log in
What are the differences between these constructs in JavaScript?
Hi all!
Please explain, please.
There are such constructions:
function Person (name, age) {
this.name = name;
this.age = age;
this.getName = getName;
}
function getName() {
return this.name;
};
var Mike = new Person('Mike', 24);
var John = new Person('John', 30);
Mike.getName === John.getName;// true
function Person (name, age) {
this.name = name;
this.age = age;
}
Person.prototype.getName = function () {
return this.name;
};
var Mike = new Person('Mike', 24);
var John = new Person('John', 30);
Mike.getName === John.getName;// и здесь true
Answer the question
In order to leave comments, you need to log in
1) Everything should be in its place, sorted out, this is the basis of the JavaScript code. This is justified by the fact that several people can work on the code, and if everyone has their own mess, then in the end someone can do this:
function getName(){...} //вы объявили функцию
//code
//code
function getName(){..} //Еще какой-то господин объявил функцию
//Что в итоге? -Объявлена будет вторая.
function Person (name, age) {
this.name = name;
this.age = age;
}
Person.prototype.getName = getName;
function getName() {
return this.name;
};
var Mike = new Person('Mike', 24);
var John = new Person('John', 30);
Mike.getName === John.getName;// true
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question