Answer the question
In order to leave comments, you need to log in
Why is 'prototype' needed in node.js and just js?
Misters experts help to deal with this 'prototype'.
I really can't figure out why it's needed.
I look at the code example and wonder why exactly the prototype was used,
are there any alternatives in this example, and how would these alternatives be better / worse?
File 1:
var ru = require('./ru.json');
function User(name) {
this.name = name;
}
User.prototype.hello = function (who) {
console.log(ru.Hello + ", " + who.name);
};
exports.User = User;
var user = require('Файл 1');
var vasya = new user.User('Вася');
var petya = new user.User('Петя');
vasya.hello(petya);
Answer the question
In order to leave comments, you need to log in
In this case, in the readability and relevance (freshness) of the code. I don’t understand people who write in ES5 in 2017 at all. Learn the ECMAScript 7 standard.
Example:
File 1:
import ru from ./ru.json';
export default class User {
constructor(name) {
this.name = name;
}
hello(){
console.log(`${ru.Hello}, ${this.name}`);
}
}
import User from './User';
let vasya = new User('Вася');
let petya = new User('Петя');
vasya.hello();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question