Save the result of a method in a class

user14320231

I want to save or store the outcome of a property in a class to use in another property in the same class. How do I do that?

for example:

class MyClass {

  get randomNumber() {
    let number = Math.floor(Math.random() * 10) + 1
    return number
  }

  get newSum() {
    return this.randomNumber + 1 // I want to use the result from the previous property and add 1 to that
  }

}

```
Emiel Zuurbier

Instantly call the randomNumber getter in the constructor. The contructor is called when you use the new keyword to create an instance of MyClass.

The newSum getter now returns the stored random number + 1.

Though, I would recommend not using the getter pattern for the newSum property as a getter should not be used to mutate values. Instead make it a proper method.

class MyClass {

  constructor() {
    this.number = this.randomNumber;
  }

  get randomNumber() {
    let number = Math.floor(Math.random() * 10) + 1
    return number
  }

  newSum() {
    return this.number += 1;
  }

}

const instance = new MyClass();
console.log(instance.number); // The random starting number.
console.log(instance.newSum()); // +1
console.log(instance.newSum()); // +1
console.log(instance.newSum()); // +1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get the result of the method and save it to a variable

Save a query result on a custom class

Cache a result set for a class method?

How to save class method in database?

Save result of async method in variable in Kotlin

Method result not save in memory how does that work?

Assign class method result to constructor parameter

'Self' is only available in a protocol or as the result of a class method

StdRegProv WMI Class and the GetStringValue method not returning a result

Call a method inside of class to save Object in a dictionary

How to save a method in a class to be executed later

Save Method of Application class failed when trying to save an Excel Document

How to save Vert.x WebClient result and return it in method

Faster when I save the result of a method in a new variable?

Call method in Parallel.Foreach and save the result into the thread safe list

Save Result As At

Java save thread result in a variable from main class

How to use a result object from Jpa repository save() later in the same method that invoked save()?

make a network request, save the result, and pass the result into main class in react-native

What is the return type for JPA query method when query result is not a class?

How can i bind a WPF control to the result of a class method?

Generic Interface/Implementation class/Method returns different result set

How to draw and save a subplot figure as a new class method in Python?

Can I save a PHP class method output into a variable

Is it a good convention to define save method inside hibernate/jpa entity class?

One method to save entity which extand abstract class in repository

The method Save isn't defined for the class State ? || Flutter/dart

The interface class method does not save the file to the selected folder

Reference the result of a method in a method