Building Objects Flashcards
What is new and what does it do?
new is actually an operator.
When we call new, immediately an empty object is created and then it invokes the function next to it. A very important point is that the this keyword now belongs to that empty object and not the function.
What is a function constructor?
A normal function that’s used to construct objects.
The this variable points to a new empty object, and that object is returned from the function automatically.
What’s the ideal way to set up properties and methods on a function constructor?
Properties should be set up in the constructor itself, but methods should be set up on the prototype. Properties will probably be unique to each object, so it makes sense to store them within each object. However, methods are most likely going to be the same for every new object. As more and more objects are constructed, this approach willl save memory space because they’ll all be pointing to just one prototype method instead of possibly thousands of copies of the same method.
How do prototypes work in function constructors?
The Function object has a special prototype property, whose only purpose is to be used on function constructors to provide a global prototype property to all objects created by that constructor - it isn’t used for anything else by the Function object.
It’s a confusing name because you might think you’re accessing the prototype of this object, like by using __proto__. But, the prototype property of the function is not the prototype of the function - it is the prototype of any objects created if you’re using the function as a function constructor.
What happens if you forget the new operator when invoking a function constructor?
It will return undefined. Since a function constructor is still just a function and the JS engine doesn’t magically know your intent, it will just execute that function as it is. Properly written function constructors don’t have return statements in their code(because they auto-return the object). So, when a function constructor is invoked without the new keyword, it has nothing to return.
Why do we capitalize the names of function constructors?
It’s helpful to differentiate them, especially because of how critical the new keyword is. If you’re getting a bunch of errors and you notice a capitalized function call without a new, then you know that’s probably the source of your errors. Linting programs will catch this for you, but the JS engine won’t, so it’s helpful to use this capitalization approach.
What is the difference between:
3 and var a = new Number(3);
“jack” and var jack = new String(“jack”);
Although these look like the same thing, the left side is a primitive and the right side is actually a special object with the primitive value boxed inside of it, along with all the special methods that come from the prototype of that object. Each of them are actually objects made by Javascript’s built-in function constructors - Object(), String(), Number(), Boolean(), Array(), RegExp(), Function() and Date().
Given:
var a = 3;
var b = new Number(3);
What will these return and why?
a == b
a === b
a == b returns true
a === b returns false
The == operator will coerce a and b to the same type and then return true
The === operator will see that a is a primitive and b is an object created by a function constructor and return false.
Why is using for in loop avoided on arrays in JS?
Because arrays are actually objects and their items are added properties. Looping through the properties may actually include other properties that have been added to the prototype. It’s safe to a standard for loop.