Coding question andrej

Why in this code:

// 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


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?



1 Like

Whenever you see function written from capital letter there is a good chance it is a constructor function.

Constructor functions create objects (instances of those functions), like this:

const array = new Array(1, 2, 3);

Keyword this in constructor function refers to instance. So in your code further down if I write:

const w = new Window([tab1, tab2]);

this instance w will have property tabs with all the tabs I’ve passed to constructor

Thanks but, first of all in my code the Window function only took one argument and not two. And second my question was where did the otherWindow constructor function get created in this code:

I dont understand why the code says “otherWindow.tabs”

this.tabs = this.tabs.concat(otherWindow.tabs);

:point_up: This is one array

otherWindow is an argument of join function, that takes this instance and rewrites own tabs (this.tabs) adding tabs of the otherWindow instance

1 Like

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

How does it take this instance and rewrite its own tabs?

Wouldnt it need to be

var otherWindow = new Window()

How does it take this instance and rewrite its own tabs?

Wouldnt it need to be

var otherWindow = new Window()

I don’t understand the question.

Window is a constructor it takes an array and adds the array to the this object on the tabs property.

var Window = function (tabs) {
  this.tabs = tabs;
  // { tabs: [ 'GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp' ] }
  // { tabs: [ 'FB', 'Gitter', 'Reddit', 'Twitter', 'Medium' ] }
  // { tabs: [ 'Netflix', 'YouTube', 'Vimeo', 'Vine' ] }

var workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']);
var socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']);
var videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']);

In this code

var tabsAfterIndex = this.tabs.splice(index + 1); // Get the tabs after the tab

Does the mean starting at index + 1 and going to the end?

Did you look at the MDN docs?

1 Like

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;

Function parameters technically only called when they are function. You probably mean “used”. Nothing will happen - you just won’t use it.

Hint: I’m pretty sure that tab parameter should go into .push() method

join() expects instance of Window. You would first create instances:

const oneWindow = new Window(tabs);
const twoWindow = new Window(otherTabs);

And then at some point you would join them:


join() method presumes that otherWindow argument will already be an instance of Window constructor

Yes it means from the index+1 position all the way to the last item in the array.

.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. hopefully it helps.