Home>
I do n’t know the process

The following two codes are simplified usingObject.definePropertyfor the example using the accessor method and the first example.
In the case of the accessor method of the first code, it was very easy to pass a value to the setter method and call_basewith the getter method. (In the sense of processing from top to bottom)

But it's fine until the second code passes the value tot.base, but the getter is listed first and the setter is later, I can understand how the value is passed from the setter method to the getter method because it is after the code, but I don't know what processing is done in what order.

My idea is two

・ In the first place, order of accessor method is setter → getter? Is there?
Is an empty_baseprepared in the getter, and the_basein the getter is also reflected by the_base = baseof the setter?

Two points. Thanks for your response.

First code

'use strict';
{
    function Triangle () {
        // private property
        var _base;
        var _height;
        // private method
        var _checkArgs = function (val) {
            return (typeof val === 'number'&&val>0);
        }
        // Method for accessing private members
        this.setBase = function (base) {
            if (_checkArgs (base)) {_base = base;}
        }
        this.getBase = function () {return _base;}
        this.setHeight = function (height) {
            if (_checkArgs (height)) {_height = height;}
        }
        this.getHeight = function () {return _height;}
    }
    // Method to find the area of ​​a triangle
    Triangle.prototype.getArea = function () {
        return this.getBase () * this.getHeight ()/2;
    }
    var t = new Triangle ();
    t.setBase (10);
    t.setHeight (2);
    console.log (t.getBase ());// 10
    console.log (t.getHeight ());// 2
    console.log (t.getArea ());// 10 * 2/2 = output 10
}


The second code is simplified usingObject.definePropertybelow.

'use strict';
{
    function Triangle () {
        var _base;
        var _hight;
        Object.defineProperty (
            this,
            'base',
            {
                get: function () {
                    return _base;
                },
                set: function (base) {
                    if (typeof base === 'number'&&base>0) {
                        _base = base;
                    }
                }
            }
        );
        Object.defineProperty (
            this,
            'height',
            {
                get: function () {
                    return _height
                },
                set: function (height) {
                    if (typeof height === 'number'&&height>0) {
                        _height = height;
                    }
                }
            }
        );
    };
// Method to find the area of ​​a triangle
    Triangle.prototype.getArea = function () {
        return this.base * this.height/2;
    }
    var t = new Triangle ();
    t.base = 10;
    t.height = 2;
    console.log (t.base);// 10
    console.log (t.height);// 2
    console.log (t.getArea ());// 10 * 2/2 = output 10
}

Reference: JavaScript introductory (revised new version) 5.4.2

  • Answer # 1

      

    But it's fine until you pass the value tot.basein the second code, but the getter is listed first and the setter is later

    The order does not matter.setis called when assigning to a property, andgetis called when referring to a property.

Related articles

Trends