Day 68 JavaScript Function Constructors

Function constructors, something that was created to appeal to Java developers to seem like JavaScript was similar. Function constructors are made by typing new then the primitive type you want to create. For example new string or new number, which is similar to other programming languages. The word new is an operator. However, the new operator creates an object unlike in Java it describes an object.

Creating a new object is beneficial in the sense that every gets a prototype so you can give multiple things a change at once.

In good code you can utilize putting methods(objects with functions) on the prototype. From an efficiency standby point you can have hundreds of object properties but only need a method once to change them all. This saves memory space.

However, function constructors are dangerous. They can be dangerous because they require the “new” operator in front of it. Without the word new in front of the function constructor, it will just return undefined. The JavaScript engine will not be able to read it or know if it is a Boolean, number, string, etc. Function constructors are always objects, meaning that whatever you make will lose its primitive value even if you weren’t intending on it to be.

These problems arise from JavaScript trying to please other developers coming from different languages to try to mimic their languages. Other languages utilize classes however JavaScript has its own way to be powerful without classes.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Up ↑

%d bloggers like this: