[Javascript] Objects vs. Classes

Philip Thompson philthathril at gmail.com
Fri Dec 11 09:36:39 CST 2009


On Dec 11, 2009, at 9:27 AM, Matt Warden wrote:

> Philip, this might help:
> http://www.phpied.com/3-ways-to-define-a-javascript-class/
> 
> On Fri, Dec 11, 2009 at 10:11 AM, Philip Thompson
> <philthathril at gmail.com> wrote:
>> Hello all.
>> 
>> I currently use Mootools as my javascript library. When I was first learning it, I instantiated classes to perform certain related classes. Then I shifted to just creating objects. What's the main difference between the two?
>> 
>> classes:
>> var SomeClass = new Class ({
>>    Extends: [Options, Events],
>>    options: {...},
>>    aMethod: function () {...},
>>    anotherMethod: function () {...}
>> });
>> 
>> js objects:
>> var SomeObject = {
>>    someId: '...',
>>    someMethod: function () {...},
>>    anotherMethod: function () {...}
>> };
>> 
>> Essentially, a class is a javascript object (in the terminology I'm using here, at least). With the class, you can extend and implement other classes (at least with this particular library). What are the other differences? Speed? Memory? Etc...
>> 
>> Thanks,
>> ~Philip

That does seem to shed some light on the subject. Basically, the "object" I mention above is the shorthand way of creating an object/function and it's a Singleton. There can be multiple instances of the "class" above. I suppose if I don't need the extra functionality that the (Mootools) class provides and I only need 1 instance, I can just create an object.

Thank you!
~Philip


More information about the Javascript mailing list