R
R
Richard Hendrix2017-08-22 22:09:48
JavaScript
Richard Hendrix, 2017-08-22 22:09:48

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;

File 2:
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

1 answer(s)
I
Islam Kiyasov, 2017-10-01
@kiyasov

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}`);
    }
}

File 2:
import User from './User';

let vasya = new User('Вася');
let petya = new User('Петя');
vasya.hello();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question