473,385 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Why the different way to declare a class?

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
4 1255
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
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
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
Ray

Paul wrote:
<snip>

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

Cheers
Ray
>

Thank you!
Ray
Aug 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: joe | last post by:
hi, after reading some articles and faq, i want to clarify myself what's correct(conform to standard) and what's not? or what should be correct but it isn't simply because compilers don't...
2
by: Sergey Ilinsky | last post by:
Well, I've been working with JS for three years and have a great experience here. But! I still have no really acceptable answer to the following question: What is the principle difference between...
13
by: Dan Tsafrir | last post by:
is the following code standard? (cleanly compiles under g++-4.0.2): struct Asc { bool operator()(int a, int b) {return a < b;} }; struct Des { bool operator()(int a, int b) {return b > a;} };...
5
by: Wysiwyg | last post by:
I'm new to c# programming and can't figure out how to avoid duplicating common code in multiple classes when I'm restricted to using different system base classes.. I'm using c# in asp.net to write...
1
by: ctor | last post by:
Hi, I'm experimenting with using a lot of different namespaces in my current project to see if it helps me keep my code more organized. In some ways I'm finding that it causes more problems...
1
by: toton | last post by:
Hi, I have two namespace contains class InkFrame and PrefDialog respectively. PrefDialog needs InkFrame to show the dialog over the frame. It also stores a pointer to InkFrame inside it. Now I...
13
by: toton | last post by:
Hi, I have some enum (enumeration ) defined in some namespace, not inside class. How to use the enum constant's in some other namespace without using the whole namespace. To say in little...
18
by: MajorSetback | last post by:
I am using the Redhat version of Linux and GNU C++. It is not clear to me whether this is a Linux issue or a C++ issue. I do not have this problem running the same program on Windows but...
1
by: sunshine19992 | last post by:
Not sure if others have come acrossed this bit I have a program for a C# class I am taking and during my troubleshooting I have found that if I turn on a breakpoint and then press F5 to continue...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.