By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 828 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

Why the different way to declare a class?

P: n/a
Ray
Hello,

While reading another guy's I found myself a bit confused by his way of
declaring his class--I usually declare a class like this:

function Foo() {
this.blah = true;
this.bleh = false;
}

Foo.prototype.blabla = function() {
};

and so on. However in his code he does a lot of this:

var Foo = {
blah : true,
bleh : false,
blabla : function() {
}
};

My question is, how are they different? I mean I guess he's declaring
his class JSON style. But can he even do a new() on his class? Seems
like then he's limited to using his methods and variables statically,
e.g.: Foo.blah, Foo.bleh, Foo.blabla() and so on.

Is there any particular reason why one would do that?

Thank you!
Ray

Jul 31 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Different things for different uses.
Ray wrote:
Hello,

While reading another guy's I found myself a bit confused by his way of
declaring his class--I usually declare a class like this:

function Foo() {
this.blah = true;
this.bleh = false;
}

Foo.prototype.blabla = function() {
};
With this technique you can make lots of foo (really instances of Foo).
var a = new Foo();
var b = new Foo();
You could also create a Foo sorting function:
function fooSort(foo1,foo2){
var comp = 0;
if(foo1.blah && !foo2.blah){
comp = (-1);
} else if(!foo1.blah && foo2.blah){
comp = (1);
}
return comp;
}
and sort an array of foo:
var a = new Foo();
var b = new Foo();
b.blah = false;
var c = new Foo();
var fooArray = new Array(a,b,c);
fooArray.sort(fooSort);

Some might avoid prototype using a form like:
function Foo() {
this.blah = true;
this.bleh = false;
this.blabla = function() {
};
arguments.callee.fooSorter = function fooSort(foo1,foo2){
var comp = 0;
if(foo1.blah && !foo2.blah){
comp = (-1);
} else if(!foo1.blah && foo2.blah){
comp = (1);
}
return comp;
}
}
// test
var a = new Foo();
var b = new Foo();
b.blah = false;
var c = new Foo();
fooArray.sort(Foo.fooSorter);
var res = "";
for(var i=0;i<fooArray.length;i++){
res += fooArray[i].blah + "\n";
}
alert(res);
>
and so on. However in his code he does a lot of this:

var Foo = {
blah : true,
bleh : false,
blabla : function() {
}
};
This form is used to create a single object. It is also useful for many
things.
If you only need one, then this is more concise.
>
My question is, how are they different? I mean I guess he's declaring
his class JSON style. But can he even do a new() on his class? Seems
like then he's limited to using his methods and variables statically,
e.g.: Foo.blah, Foo.bleh, Foo.blabla() and so on.

Is there any particular reason why one would do that?
Let the flame wars commence... ;-)
>
Thank you!
Ray
Jul 31 '06 #2

P: n/a
In article <11**********************@s13g2000cwa.googlegroups .com>, Ray
<ra********@yahoo.comwrites
>Hello,

While reading another guy's I found myself a bit confused by his way of
declaring his class--I usually declare a class like this:

function Foo() {
this.blah = true;
this.bleh = false;
}

Foo.prototype.blabla = function() {
};

and so on. However in his code he does a lot of this:

var Foo = {
blah : true,
bleh : false,
blabla : function() {
}
};
<snip>

If you want only one of that kind of object then the second way is less
typing. If you want several then the first way is less confusing and
often more convenient.

Incidentally, if you are using 'class' to mean a class of objects with
something interesting in common then ok.

If you mean objects with a rigid class definition that can't be altered
after construction, as in Java, then very not ok.

Either way, 'class' is a dangerous word to use in Javascript.

John
--
John Harris
Jul 31 '06 #3

P: n/a
Ray
John G Harris wrote:
If you want only one of that kind of object then the second way is less
typing. If you want several then the first way is less confusing and
often more convenient.

Incidentally, if you are using 'class' to mean a class of objects with
something interesting in common then ok.
Um, yeah, class as in a class of objects, not like in Java :)

Thanks John!
Ray
>
If you mean objects with a rigid class definition that can't be altered
after construction, as in Java, then very not ok.

Either way, 'class' is a dangerous word to use in Javascript.

John
--
John Harris
Aug 1 '06 #4

P: n/a
Ray

Paul wrote:
<snip>

Hey, thanks Paul! I learned a new trick :)

Cheers
Ray
>

Thank you!
Ray
Aug 1 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.