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_base
with 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.
・ In the first place, order of accessor method is setter → getter? Is there?
Is an empty_base
prepared in the getter, and the_base
in the getter is also reflected by the_base = base
of 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
Related articles
- javascript - i'm new to gulp gulp processing is not executed
- javascript - i want to put the data acquired by asynchronous processing in the property of the class
- javascript - i want to change the process using check in jquery
- javascript - about the process when making an associative array an object
- javascript - i want to add processing when the setter is called for all properties
- javascript - iterative processing does not work with jquery
- javascript - quotation escape process doesn't work
- javascript - about processing when you want to pass a query to the url at the time of page transition on the website
- javascript - return value of processing in dialog with ajax
- javascript - about the processing mechanism of the function
- javascript - about the order of push processing
- javascript - when implementing current navigation with jquery, if there are two or more of the same url, the active class will b
- i'm stumbling on asynchronous processing of javascript
- asynchronous processing - in the dart asynchronous process, the return value unexpectedly becomes future type
- [javascript] [react/redux] what is the process described in export of connect?
- nodejs - node: in asynchronous processing, i want to process each of a large number of elements it will be processed randomly
- javascript - about sequential processing of firestore of firebase
- javascript - about the problem that the same process is performed but the result is different
- javascript - i want to repeat a function that has event processing
- javascript - how to write the process to put the id of the clicked element in a variable
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- javascript - how to check if an element exists in puppeteer
- xcode - pod install [!] no `podfile 'found in the project directory
- i want to call a child component method from a parent in vuejs
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning
The order does not matter.
set
is called when assigning to a property, andget
is called when referring to a property.