Javascript prototype syntax - variable name starting with underscore

I am following an online tutorial and am on a few JavaScript codes that I am having a hard time understanding with. There is a function called Note () defined in javascript. Below is the code to add getters and setters in the prototype section. I don't understand why the _id variable is underscored in front of it? What is the purpose of underlining and when is it used?

Note.prototype = {
    get id() {
        if (!("_id" in this))
            this._id = 0;
        return this._id;
    },

    set id(x) {
        this._id = x;
    },

    get text() {
        return this.editField.innerHTML;
    },

    set text(x) {
        this.editField.innerHTML = x;
    }

      

+3


source to share


1 answer


I narrowed down the issues and I repeated this question.

It turns out that "name" and "_name" are two completely separate variables. If we use "name" in setters and getters, it results in an infinite recursive function call, so it doesn't work as expected.



You can find the answer to this question with more information here.

Javascript getters and setters - recursion problem

+1


source







All Articles