3

I'm a beginner w/ Javascript. I'm looking at the following code that someone else wrote:

function MeetingPage()
{
   MeetingPage.colors = new Object();
}

...

var meeting = new MeetingPage();

From what I've seen, I believe that the MeetingPage function creates an object that later someone holds onto in meeting. What is the MeetingPage.colors? Is the MeetingPage prefix some kind of global? Is it a "this" pointer of some kind?

Any suggestions would be appreciated.

5
  • Usually you see this there, I'm not sure if that will work the same (or even if it intends to). Commented May 8, 2011 at 1:05
  • The code seems rather weird. Essentially the colors property will always be changed each time you invoke MeetingPage as a constructor or function. Also the colors property won't even be linked to an instance of MeetingPage. Commented May 8, 2011 at 1:06
  • How would you link the property to an instance? Commented May 8, 2011 at 1:17
  • 2
    with this. instead of MeetingPage. Commented May 8, 2011 at 1:18
  • 1
    To make it instance-based instead, it would be this.colors = {}; instead. Commented May 8, 2011 at 1:18

3 Answers 3

8

It's actually just bad code. MeetingPage.colors = new Object(); is setting a property called colors on the MeetingPage function, i.e:

function MeetingPage(){ }
MeetingPage.colors = {};

Which is perfectly valid since all functions in JavaScript are objects. The problem is that if you have multiple instances of meeting page:

var meeting1 = new MeetingPage();
var meeting2 = new MeetingPage();

The code you posted will reset colors. It should either should be written as this.colors = {}, or it should be set outside of the function as in my first snippet.

Sign up to request clarification or add additional context in comments.

Comments

0

This talk was really helpful when I got into different object patterns in javascript. Example code is included in the second link (the video introduces it).

http://alexsexton.com/?p=94

http://alexsexton.com/inheritance/demo/

http://alexsexton.com/?p=51

Of course, you should also definitely read http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742

HTH

Comments

0

This is the JavaScript syntax for creating Class Properties. Note, it is a class property not an instance property, that means it is shared across all instances of the class. (If you know C++ this is like a class static) However, I didn't think it was valid to put a class property inside the constructor itself. I would think that every time a new MeetingPage is created the colors class property will get wiped out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.