Based on your response, I think I see what’s still confusing you.
To clarify the terminology of what @DanCouper said, in object-oriented programming, we model real-world objects by storing relevant information about them, as in the syntax-free example below:
Car: {
function: transportation;
wheels: 4;
}
Bicycle: {
function: transportation;
wheels: 2;
}
Because these two objects have similar information (properties/component variables that are set to the same value), we can say that in our model, they belong to the same class of object. That class could be something like:
Class Vehicle {
function: transportation;
}
which allows us to say that a Car
or a Bicycle
is an INSTANCE of the class of objects that are Vehicle
s.
In the example code I wrote in my previous reply, there are 3 objects involved, 2 of which are mentioned explicitly, and one of those 2 is an instance of the class of Function objects, as you can see if we map the variable names onto simpler names.
let myNewWindow = new Window(['first tab', 'second tab']);
let A = new B();
A
is a Javascript variable, and it can hold anything. Its type
is determined by what we assign to it. In this case, that would be the result of the expression new B();
B
is a function. As in all math and computing, functions return values based on what their input parameters were. In this case, we pass in no parameters, but B will return a value, even undefined
if no explicit return value was called, as in:
function B(){};
let testReturnValue = B();
console.log("The value returned from 'blank' function B is:");
console.log(testReturnValue); // expected output: undefined
CodePen with above code
What’s important to realize in the current discussion is that B, while being a function in the general sense of functions, is a function in Javascript. And all functions in Javascript are first-class functions, which means they can be treated like any other data (assigned as the values of variables, like the function assigned to the variable Window
in the lesson example), or passed as parameters to other functions (the definition of a callback). In Javascript, such values have to be one of the seven data types: 6 primitives (number, string, boolean, null, undefined, symbol) or an Object. Since a function clearly can’t be represented by one of the 6 primitive data types, it has to be an Object. The following picture shows the relevant expanded browser console output from a slightly different pen to show Functions are Javascript objects.
Link to the pen - Note: The codepen console is insufficient to demonstrate what is necessary.
And finally, object C
is not listed in the source code: let A = new B();
but this “hidden” object exists. It is the one that was copied by the new
operator. In addition to copying C
, new
also assigned a reference to the copy as the value of this
to any source code inside the Function object B
, so that any code that alters this
will be altering the copy. new
knows which object to copy because as an object, the function B
has properties (key/value pairs), and one of those properties stores a reference to the object to be copied, the prototype of all B
s. In the picture above, Firefox’s console lists this as the prototype
property of the function object B
. All functions have a prototype
property, so all functions can be used as constructors, although by convention, we Capitalize the ones that we intend to use as Constructors.
Now, let’s summarize with specifics: A
is an instance of the class of B
objects. All instances of B
s are created by calling the new
operator on the constructor function B
, and start their lives as copies of the prototypal B
object, C
, which then have the commands inside the constructor function B()
run upon them.
Further reading:
MDN - first class functions
MDN - “Function” constructor - the construction function that makes new functions