detect if a function is called as constructor in JavaScript
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
JavaScript
🤖 Code Explanation
This code is written in JavaScript. It is a constructor function for a class named Foo. The function contains a check to see if the caller used the keyword new when invoking the constructor. If the keyword new was not used, the constructor function returns a new instance of the class. Otherwise, the constructor function initializes the value property of the new instance to 10.
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
More problems solved in JavaScript
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)
![](https://cdn-icons-png.flaticon.com/512/5968/5968292.png)