Chain new Class() and function to the same variable in JavaScript?

javascript class
javascript class inheritance
prototype chain javascript
javascript inheritance w3schools
javascript prototype vs class
javascript class destructor
javascript instantiate class
javascript prototype explained

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.

Inheritance and the prototype chain, JavaScript does not have "methods" in the form that class-based languages define them. In JavaScript, any function can be added to an object in the form of a property. var o = { a: 2, m: function() { return this.a + 1; } }; console.log(o.m()); [[Prototype]] is the value of Graph.prototype when new Graph() is  The code declared with function and class both return a function [[Prototype]]. With prototypes, any function can become a constructor instance using the new keyword. const x = function() {} // Initialize a constructor from a function const constructorFromFunction = new x(); console.log(constructorFromFunction);

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()

Details of the object model, An instance has exactly the same properties of its parent class (no more, no Any JavaScript function can be used as a constructor. In a class definition, you can specify that the new class is a subclass of an Inheritance model, Inherit properties by following the class chain. var mark = new WorkerBee;. Now, if we run the new code, the variable tmp will reference the same object as the variable bob, like so: let bob = new Kitten (); let tmp = bob. setName ('Bob'); tmp. setColor ('black'); console. log (tmp === bob); // OUTPUT: // > true Now we can remove the tmp variable and chain our method calls directly. Here’s the final product:

someFunc is just a variable here.

new someClass() would get called every time if you wrote someFunc as

const someFunc = () => new someClass().getOrSetWhatever()

Understanding Method Chaining In Javascript, Writing a single function that defines all these actions is usually a terrible idea, so we write a… it depends on and so on till we get to the first function in the chain. from the same class by writing new SomeClassWeWrote() , their values the results of their computations in an instance variable, this.value . However, things get a bit more complicated when extending classes so I would suggest that if you use a properties() function in all of your classes to declare you class properties that you prefix the method name with the class name (IE: MyClassProperties() in order to avoid accidentally overriding function calls within sub classes.

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.

Object-oriented JavaScript: A Deep Dive into ES6 Classes, Jeff Mott takes an in-depth look at ES6 classes starting with the Methods defined in the constructor capture variables in a closure this. Symbols are a new feature of JavaScript as of ES6, and they give us Look again at getDay() . of a class that needs the same data and functions as another class. 'variableObject': {// contains function arguments, inner variable and function declarations} Scope Chain. In the creation phase of the execution context, the scope chain is created after the variable object. The scope chain itself contains the variable object. The Scope Chain is used to resolve variables.

Understanding JavaScript Constructors, Technically, JavaScript doesn't have classes, but it has constructors and A constructor is like a cookie-cutter for making multiple objects with the same a number object // numbers have a toFixed() method var obj = new Object(5); obj. Wrapper(); } var dog = new Dog(); // this call will traverse the prototype chain until it is  JavaScript classes, introduced in ECMAScript 2015, are primarily syntactical sugar over JavaScript's existing prototype-based inheritance. The class syntax does not introduce a new object-oriented inheritance model to JavaScript.

Pro JavaScript with MooTools, The Chain mixin adds a special method called chain to classes that this function in an internal call queue that can be accessed later. var Phone = new Class({  First, a function created by class is labelled by a special internal property [[FunctionKind]]:"classConstructor". So it’s not entirely the same as creating it manually. The language checks for that property in a variety of places. For example, unlike a regular function, it must be called with new:

Comments
  • 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 function expression!
  • 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.
  • Only if getOrSetOrWhatever is muatable and changed between calls. In Javascript nothing can be same and everything could be 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.