WebFX WebBoard
Message Thread:
View All Messages
Back to WebFX
 Favored way to create Objects in JavaScript, Jeff Lee, July 6, 2003
     Re:Favored way to create Objects in JavaScript , Tim Scarfe, July 8, 2003
     Re:Favored way to create Objects in JavaScript, Erik Arvidsson, July 6, 2003

Subject: Re:Favored way to create Objects in JavaScript From: Tim Scarfe Date: July 8, 2003

Literal object constructors should generally be avoided as they are not clear enough. They can be useful for struct/namespace type functionality.

As Erik said, they are never a replacement for a class/constructor.

Tim

From: Jeff Lee
Sent: July 6, 2003
Subject: Re:Favored way to create Objects in JavaScript

Erik,

As there are several different ways to create Objects in JavaScript, I can't seem to decide what is the best way.

You can create the Object directly using something like:

var MyObj = {
  myProperty: value,
  anotherProperty: value,
  myMethod: function () {},
}

I like this way because the object is created and can be used right away, but you can only have one.

Or use a constructor:

function MyObject() {
  this.myProperty = value;
  this.anotherProperty = value;
  this.myMethod = function () {};
}
var MyObj = new MyObject();

I'm guessing this is a better way because it's more object oriented. But in JavaScript, when you're only building one object anyway (for example), why go thru the extra trouble?

I'm just looking for your opinion.

Jeff





Reply
Name: 
Email: 
Subject: 

Enter your reply to this message below. HTML tags are not supported but words that start with http://, ftp:// or mailto: are converted to links.


View All Messages
Back to WebFX