By creating a new instance of someClass() and chaining a function to the same variable, will a new someClass be created every time I call someFunc()?
const someFunc = new someClass().getOrSetOrWhatever()
compared to if I write it as is usually done
let s = new someClass() s.getOrSetOrWhatever()
And if I put the first example into a constructor, would it mean that there would be only one instance (everytime I called this.someFunc)? Would it be bad practice? Thanks in advance!
Every time you call
new someClass() a new instance of someClass is created. The "new" keyword causes a unique "copy" of the class to generate an new, unique copy (instance).
When you execute
getOrSetOrWhatever() chained to
new someClass() as:
let s = new someClass().getOrSetOrWhatever()
Then the "s" variable will contain the result of getOrSetOrWhatever (whatever that function returns).
HOWEVER, the instance that
new someClass() generated will exist, but not be accessible because it was never assigned to a variable (potential memory leak) -- only the result of getOrSetOrWhatever is returned.
Chaining passes whatever is generated/returned on the left to the right.
foo().bar() // is like: // foo() --> bar() // or // the method "bar" is within "foo"
When you don't chain as in:
let s = new someClass(); let foo = s.getOrSetOrWhatever();
Then the instance is assigned to the
s variable and
foo is assigned to whatever "getOrSetOrWhatever" returns.
if you write this like:
const someFunc = new someClass().getOrSetOrWhatever;
than someFunc will be reference (pointer) to method (function) of that instance of someClass, after if you call:
const someFunc = new someClass().getOrSetOrWhatever; someFunc(); ... someFunc();
is same as:
let s = new someClass() s.getOrSetOrWhatever() .. s.getOrSetOrWhatever()
someFunc is just a variable here.
new someClass() would get called every time if you wrote
const someFunc = () => new someClass().getOrSetWhatever()
No, a new instance will not be created every time because the code "new someClass()" will not be called again
You have declared a variable named "someFunc".
The right side of your code is going to be evaluated, in order to assign a value to "someFunc"
When the right side of your code is read, it will first create a new instance. If you did not have any more code on the right side, it would simply store that new instance to the variable.
But, since you are then calling a method called "getOrSetWhatever", the result of "getOrSetWhatever" will be stored to your variable.
Your instance is now just floating in memory somewhere and is not stored to any variable.
You have created an instance, invoked a method (that is either stored on that instance or stored on the class's prototype object), and stored the result of that method into your variable.
Node.js for PHP Developers, Ifthe Node.js chain ofprototype objects is correctly set up, the Node.js class object variables and functions in the same way that the PHP traditional class-based of a new empty object that is created using the Node.js Object.create() function A class is a type of function, but instead of using the keyword function to initiate it, we use the keyword class, and the properties are assigned inside a constructor() method. Class Definition Use the keyword class to create a class, and always add the constructor() method.
- What do you mean by "every time I call someFunc()?" - does
getOrSetOrWhatever()really return a function? Please show its code. In the code you've shown, there is no
- Why would a new instance be created when you call
someFunc? The line with
new someClass()has already been executed, and is not inside the body of the function...
- With what you suggest in the last paragraph, do you mean that the constructor would need to be called repeatedly? In that case, you would need to create an instance for each time you want the constructor to run.
- Is .getOrSetOrWhatever a method that is on the prototype? If so, you do not need to create a new instance at all.
- Thank you bob, this was exactly what I wanted to know! Thanks for answering my question!
- This is similar but not always the same.
- This is not what the OP is doing. His code does
someFunc = ….getOrSetOrWhatever().
- Comments are not for extended discussion; this conversation has been moved to chat.