JavaScript classes
OK, think of it as a religious flamewar to the likes of "tabs vs. spaces for indentation". Looks like this particular war is currently (at JSConf and NodeConf) even more heated than it should be. Classes vs prototypes. For or against classes.
I personally don't care about the "winner". The thing is that classes currently don't exist in JavaScript. No such thing. However looks like they might in the next iterations of the language (see latest post from Brendan Eich). Some people miss classes so much that they start calling other things classes or come up with some approximation. Problem is, because classes don't exist, people often mean different things when they say "class".
Sometimes they mean "constructor functions". Sometimes they mean a regular object literal (singleton-type thing. Heck, "singleton" is also open for interpretations). Sometimes they mean an object or a function defined using Crockford's module pattern.
Sometimes it's some completely different home-grown (or library-provided) thingie called klass for example (in my "JavaScript Patterns" book I have one example for educational/thought-provoking purposes). It has to be klass, or _class or something weird. Because class is a reserved word. Unused, but reserved. And one day may be full of meaning. See the problem?
I avoid saying "class". It just doesn't exist. Imagine two months down the road ECMAScript comes up with classes. And they will most certainly not be the classes you may mean today (e.g. classes won't be another name for constructor functions, I'm sure).
So any written text/blog/documentation you've produced will become incorrect and even worse - misleading and confusing.
To summarize:
- saying "class" today is confusing and takes extra effort to process (what do you mean? what was that again? it doesn't really exist and is not defined in the language, so an extra translation step is required)
- saying "class" today will read plain wrong tomorrow, will confuse and misinform
Note:
Heck, even I have this "how to define a JavaScript class" post on my other blog. I wrote it years ago when, coming from PHP, I was curious how stuff works in JavaScript. Well I got it wrong then. But fixed it not too long ago because it was top 1 result in google and Yahoo search for "javascript class" and "javascript classes" and I didn't want to continue contributing to the confusion.
Note 2:
To my regret, I couldn't make it to JSConf (aka the best!) nor NodeConf this year (because all girls and women in my life are born in May and it's impossible to travel) so I may be a little off on the level of flamewarfare, but according to Twitter I'm not.
Tags: class
May 5th, 2011 at 7:56 pm
I attended JSConf 2011, and I’m at NodeConf right now. There isn’t much of a flamewar here. It seems that everyone is on board with the notion of creating syntactic sugar for doing prototypal inheritance. There might be a ‘html5 washing’ style argument around the semantics of using the word “class”, but that’s not substantive.
Generally there is a really moving and exciting feeling in the air here around JS.next, and the future of the JavaScript community; creating facilities in the language to support a JavaScript core, and coming together to build that core.
May 5th, 2011 at 8:06 pm
I don’t know what I would use classes for that constructor functions don’t already do for me. When I’m writing native code for Node.js modules, the v8 c++ maps classes almost 1:1 to the exposed JavaScript constructors.
Introducing a class type seems kind of unnecessary.
May 6th, 2011 at 12:07 am
Well saying “classes don’t exist” in JavaScript is half true…
True that the specification says: “ECMAScript does not use classes such as those in C++, Smalltalk, or Java”
But he you read it well, it doesn’t say “does not use classes” but “does not use classes as those …”
Classes exists in JavaScript, it is just that JavaScript is not class-based but prototype-based.
In ECMAScript, the Class is an internal property, literally called [[Class]]. Its definition is “A String value indicating a specification defined classification of objects.”
Not very clear… sure its specification, not a documentation
So here more details in the section 8.6.2 (Object Internal Properties and Methods):
-> The value of the [[Class]] internal property is defined by this specification for every kind of built-in object. The value of the [[Class]] internal property of a host object may be any String value except one of “Arguments”, “Array”, “Boolean”, “Date”, “Error”, “Function”, “JSON”, “Math”, “Number”, “Object”, “RegExp”, and “String”. The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except through Object.prototype.toString (see 15.2.4.2).
When you apply this same toString function on object created via a custom constructor, the returned class is the name of the constructor function.
So finally, in JavaScript the class of an object is the name of its constructor… For the native Objects.
Thinking about it, objects created through a constructor is designed by the constructor prototype, and the result of the constructor initialization.
But well… for any other Object created with “new” the class internal value is always “Object” (see section 13.2.2 [[Construct]]). It is the same result with Object.create(proto)
That’s ECMAScript…
May 6th, 2011 at 12:10 am
Oups… error: “When you apply this same toString function on object created via a custom constructor, the returned class is the name of the constructor function.”
No it’s only on native object
but the BOM and the DOM define their own native objects
May 6th, 2011 at 10:55 am
As someone who is *definitely* guilty of using the term “class” in Javascript, I would be very hesitant to call it “misleading”. After all, if people are trying to wrap their heads around a concept, coming up with familiar terms I think can be more helpful than harmful.
We could, for instance, make a big deal about when we refer to “methods” vs. “functions” as these are different things. But, I don’t think misusing them is harmful. We are painting a picture of “behavior” and I think that is what is more important.
For example, when I talk about the prototype chain, I often talk about a “super constructor” – passing initialization off to the prototype constructor. From a technical standpoint, this is totally inaccurate since there is no “super class”. However, for people who have some classical CS training, I think talking about it in familiar terms is helpful to bridge the gap.
Now, I am not saying that we shouldn’t have a common vocabulary – we definitely should (and I wish I was better about figuring it all out). BUT, I just want to say that I don’t think some leniency in the terminology is going to be harmful.
May 6th, 2011 at 12:38 pm
Whenever I miss inheritance when working with Javascript I just $.extend().
May 6th, 2011 at 3:00 pm
I’ve resorted to calling them types: an array is an instance of the Array type. Then you can also talk about subtyping and super-types or sub-types.
September 30th, 2011 at 7:31 pm
I cant wait anymore! Yahoo Web Search…
[...]JSPatterns.com » Blog Archive » JavaScript classes[...]…
December 15th, 2011 at 12:35 pm
watch movies online free…
[...]JSPatterns.com » Blog Archive » JavaScript classes[...]…
December 16th, 2011 at 6:44 pm
Thank you for sharing with us, I think this website really stands out : D.
January 3rd, 2012 at 1:40 pm
Pretty decent blog! Have any tips on aspiring writers? I??m about to start my personal blog soon but I??m some lost on everything.
January 3rd, 2012 at 1:55 pm
i have to buy wireless speakers but it is possible to best brand?**
January 5th, 2012 at 3:12 am
HEY!Whats wrong with a little coo talk,you think its not going through the minds of people in the military after watching the rape of their country coupled with the lack of respect they get from Washington for all their hard work? Cheap GW2 Gold
January 31st, 2012 at 1:18 pm
I find one of the most confusing aspects of JavaScript to be the DOM objects, which are said to belong to various “classes” such as Element, HTMLElement, and HTMLBodyElement. These certainly aren’t true classes because JavaScript doesn’t have a ‘class’ construct. They’re not even constructors, which are the nearest thing to classes in current JavaScript. I regard these “class” designations as just a form of documentation, a label that indicates what properties a prebuilt object provides and tells you where to find a description of the object in the documentation. But what a source of confusion!
August 16th, 2012 at 2:57 pm
I haven’t attended any JavaScript conferences (reading this comments I think I should start). Regarding confusing part of the language… well I recently wrote: JavaScript Confusing Bits highlighting everything that made me go “WHAT ?!?!?!!”. There is a lot but most important are call and apply magic, no classes, !(arguments instanceof Array) and most importantly inheritance. I have seen at least of dozen ways of doing it and quite different commonly available implementations like: prototype, base, underscore, knockout… and there is more!!! AOUCH!!!
August 21st, 2012 at 2:46 am
[...] Source: http://www.jspatterns.com/javascript-classes/ [...]
January 7th, 2013 at 12:23 pm
It’ll be interesting to read about what do you think of TypeScript, which brings c-family style OOP to JS…
April 17th, 2013 at 10:43 pm
In Sonnet 18 from Elizabeth Barrett scars’s Sonnet 41 from Sonnets from the Portuguese dramatizes the closeness of the two poems that will be used to show the ability of the dramatic monologue.
May 1st, 2013 at 3:12 am
Great work! That is the variety of facts that needs to be shared online. Feel bad for on bing for no lengthier ranking this article greater! Come on in excess of along with consult with my personal site. Many thanks Implies)
May 17th, 2013 at 11:06 am
This article contains details about Classes and inheritance in javascript. A good started for object oriented javascript.
http://www.altafkhatri.com/Technical/Javascript/Inheritance-Object-Oriented-Jquery/Classes-Anonymous-Constructor-Static
May 18th, 2013 at 2:00 am
Hey very nice web site!! Guy .. Excellent .. Wonderful .. I’ll bookmark your website and take the feeds additionally?I am happy to search out so many useful information here within the put up, we’d like develop extra strategies in this regard, thank you for sharing. . . . . .
May 20th, 2013 at 5:34 pm
all inclusive cruises Gift-Giving IInsert a small doily at the bottom of each dinner
plate. The stage is able to make do with less blood flow.
Indeed, all the while learning lessons about patience.
This idea has been overtaken by a new breed of knitter.
It’s also worthwhile visiting Vailima where you can also select a combination of what are commonly known as the food processor. For further information including family planning, please visit our premiere website. Many companies provide a lot of foods in large quantities and enjoy with loved ones and really let your hair down.