Languages
[Edit]
EN

JavaScript - how to use '.apply(...)' method with (or instead) 'new' operator

4 points
Created by:
Creg
9600

In this article we would like to show how to create object with MyClass.apply(...) method instead of new operator in JavaScript.

Simple solution for modern JavaScript:

Simple solution for older JavaScript:

Note: read this article to see MyClass.call(...) method examples.

Check below alternative solutions:

Alternative solutions

Edit

There are few different ways how to achieve same effect, what was presenbted in this section.

new operator with spread syntax (...) example

Edit

In modern JavaScript, the following approach is mostly correct.

new operator with .bind.apply(...) methods example

Edit

new operator, .bind.apply(...) methods with Function class example

Edit
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join