473,729 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.b labla = 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 1273
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.b labla = 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,fo o2){
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(f ooSort);

Some might avoid prototype using a form like:
function Foo() {
this.blah = true;
this.bleh = false;
this.blabla = function() {
};
arguments.calle e.fooSorter = function fooSort(foo1,fo o2){
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(F oo.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************ **********@s13g 2000cwa.googleg roups.com>, Ray
<ra********@yah oo.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
3638
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 support. (first i compiled them with g++3.x. ERR means compiler will bark, otherwise it does accept it. Then the Comeau C/C++ 4.3.3 comes)
2
2326
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 declaring methods/properties in the constructor function body and via prototypes. Are there any real GURUs? Let's discuss the issue. P.S. Currently I am working on XUL implementation for IE, so there is
13
2533
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;} }; int main() { int arr = {1, 2, 3}; set<int,Asc> asc(arr, arr+3); set<int,Des>::iterator beg = asc.begin(); // set<int,Des>::iterator end = asc.end(); // copy(beg, end, ostream_iterator<int>(cout,...
5
2098
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 a web application but this isn't specifically a web question. I have two classes which must inherit a different System class, in a specific case my web pages must inherit System.Web.UI.Page while my user controls must inherit...
1
1793
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 than it prevents. My main issue right now is the clutter that gets added when I forward-declare classes in other namespaces, but maybe I'm not doing it right. The only way I've found to do it is as follows:
1
5414
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 want InkFrame to be forward declared in the PrefDialog header file rather than to be included. I want to include it in the cpp file instead. There is no harm including it in PrefDialog header, and it works. But I want to save some compilation time....
13
18147
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 detail, the enum is declared as, namespace test{ enum MyEnum{ VALUE1,VALUE2 };
18
2634
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 tweaking the C++ code appears to fix the problem on Linux. I have a static array that is declared privately in one class and a structure that is declared publicly in another class. The program uses both classes. I was getting core dumps and traced...
1
1594
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 until the program finishes I am receiving different results than if I didn't insert a breakpoint. (Put breakpoint at 1st curly bracket after AddToSumCount(rollSum); line in RollDice() method): Here is a copy of my code if you want to see it: using...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9281
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.