1

Disclaimer: This is a personal learning exercise, but this is NOT related to any schoolwork; I'm not even currently enrolled in a programming class of any language. I'm merely hoping to learn Javascript more thoroughly for personal growth.

I'm trying to write a page, using an existing one as a framework. I'm trying to understand existing code so I can adapt

Relevant Existing Code:

function = Page()
{
  this.items = new Array(20);
  this.itemsIndex = 0;
}

Question: I understand the first and third lines (function and new Array(20)), but I am not familiar with the syntax and purpose of "this.itemsIndex = 0". A Google search has failed me thus far, and meager learning resources (books and online tutorials) have never touched this syntax that I'm aware of.

Can anybody help me find an existing resource that explains this to somebody new (but not wholly unfamiliar) with OOP? Barring that, could somebody explain it here?

RyanLC
  • 13
  • 3
  • 1
    `itemsIndex` is just a property name, it doesn't have any special meaning in JavaScript. Moreover, doing `Array(number)` creates an array with a given size which is almost never what you need/want to do in modern JS. – Benjamin Gruenbaum Dec 20 '13 at 00:00
  • You just have to learn what `this` means in JS. – punund Dec 20 '13 at 00:02
  • 2
    `function = Page()` typo? – RobG Dec 20 '13 at 00:07
  • 1
    You might want to read about [constructor functions](https://stackoverflow.com/questions/13418669/javascript-do-i-need-to-put-this-var-for-every-variable-in-an-object/13418980#13418980) – Bergi Dec 20 '13 at 00:11
  • This may be of interest to you: http://stackoverflow.com/a/16063711/1641941 – HMR Dec 20 '13 at 05:11
  • RobG - Not a typo, no. But not my code, either. I'm trying to re-write to fit my own requirements. punund - I'm aware of "this". Just not "itemsIndex" (or rather, that got answered below, now) Bergi - Thanks for the additional reading. HMR - Thanks for the additional reading. – RyanLC Dec 20 '13 at 07:43
  • If not a typo, the code `function = Page` is a syntax error. The [function keyword](http://ecma-international.org/ecma-262/5.1/#sec-7.6.1.1) can't be used as an identifier and must be followed by either a valid identifier (a function name) in the case of a function declaration, or a possibly empty formal parameter list `()` in the case of a function expression. Can you run the code? If so, in which environment? – RobG Dec 20 '13 at 22:13
  • RobG - The code I included is part of a large program; about 20-pages long. I can run the original, but my purpose is merely to learn about parts, so I can re-write. From my research in the last 24 hours alone, I can tell that I'm pretty much going to do a full re-design and re-write. – RyanLC Dec 21 '13 at 02:27

1 Answers1

2

Given:

function Page()
{
  this.items = new Array(20);
  this.itemsIndex = 0;
}

It is a convention that functions intended to be called as constructors (i.e. with the new keyword) start with capital letter. The body indicates a constructor too, so the function should be called like:

var page = new Page();

When called with new, a function's this is set to a new object created as if by new Object() with it's private [[Prototype]] property referencing the public prototype property of the constructor function. This new object is an "instance" of Page.

So

this.items = new Array(20);

creates an items property of the new object and gives it a length of 20, and

this.itemsIndex = 0;

creates an itemsIndex property of the new object and assigns it a value of 0. Since the function does not return anything, it will return the new object by default (functions called with new must return an object).

alert(page.items.length); // 20

alert(page.itemsIndex);   // 0

alert(page.items[0]);     // undefined since nothing has been assigned
RobG
  • 142,382
  • 31
  • 172
  • 209
  • So what I understand from this is that the itemsIndex (I understand the "this" keyword) is something the original author named that particular key. If that's the case, since I'm re-writing in my own fashion, I can safely ignore the particular name chosene. – RyanLC Dec 20 '13 at 07:40