// tabs is an array of titles of each site open within the window
var Window = function(tabs) {
this.tabs = tabs; // We keep a record of the array inside the object
};
// When you join two windows into one window
Window.prototype.join = function (otherWindow) {
this.tabs = this.tabs.concat(otherWindow.tabs);
return this;
};
Is it
otherWindow.tabs
I dont get where the .tabs comes from or what it means.
Does it mean otherWindow has a property tab even though otherWindow is not defined?
otherWindow is a parameter it value is passed to the join method when it gets called
// definition, otherWindow is a parameter
Window.prototype.join = function (otherWindow) {
this.tabs = this.tabs.concat(otherWindow.tabs);
return this;
};
// method call, "videoWindow.tabClose(2)" and "workWindow.tabClose(1).tabOpen()" are passed as arguments to the "otherWindow" parameter
.join(videoWindow.tabClose(2)) // Close third tab in video window, and join
.join(workWindow.tabClose(1).tabOpen());
What happens when you have a function who’s parameter is not called. Ex.
// When you open a new tab at the end
Window.prototype.tabOpen = function (tab) {
this.tabs.push('new tab'); // Let's open a new tab for now
return this;
};
@andrejjj222
.tabs is the property of instance. You need to operate on the property of instance, not the object instance itself. That’s why you need to add “.tabs” to “otherWindow”. In fact “otherWindow” is defined in the join method function definition. It is the join method’s parameter. Later on in the code near the end, you see the join method is called, e.g. “videoWindow.tabClose(2)”, which is the argument for the parameter “otherWindow”. Note that tabClose() and tabOpen() methods all return “this”, which is the instance itself. Thus what is in the parentheses of join method is effectively referring to “otherWindow”.
for example:
join(videoWindow.tabClose(2)) ; videoWindows.tabClose() ==> otherWindow , since invoking tabClose produces a return “this”, which is in this case “videoWindow” as “otherWindow”.
I guess you were struggling to understand parameter vs argument, you may find a good reference with this link.https://www.geeksforgeeks.org/javascript-function-parameters/. hopefully it helps.