views:

317

answers:

7

I'm reading a book "How to Do Everything with JavaScript" and I'm currently learning how to define classes. The book says there are 2 ways. first using functions in javascript 1.x. second, using class in javascript 2.0.

what I'm trying is:

class Car {
var Make : String;
var Model : String;
var Year : Integer;
var Color : String;
var FullName : String;
function Car (make, model, year, color) {
this.Make = make;
this.Model = model;
this.Year = year;
this.Color = color;
this.FullName = this.Year + " " +
"<b>" + this.Make + "</b> " +
this.Model;
}
}
var mySUV = new Car("Toyota", "4Runner SR5",2001, "Thundercloud");
document.write ("I drive a " + mySUV.FullName);

The code is not working when I'm trying to run it. I use komodo editor to develop and when I define a class like I mentioned before, It gives me a warning "strict warning: class is a reserved identifier".

Is there something wrong with the code? Thanks in advance for any help.

A: 

I don't know of any browser that has currently implemented JavaScript 2.0. That might be why you cannot run it.

vrutberg
I'm sorry. It seems that the book I'm reading is bad.
codemaker
The AVM2 (FlashPlayer 9+ has implementend EcmaScript 4, which should have become JavaScript 2)
Hippo
+8  A: 

JavaScript 2.0 aka ECMAScript 4 was abandoned in 2008, before it was ever released. There will never be a class-based version of ECMAScript. Which is a good thing.

Jörg W Mittag
I personally kinda liked ES4.
PiPeep
There were some good ideas in it. But it had 3 major problems: 1) it simply wasn't JavaScript. It might have been an nice *new* language, but it wasn't JavaScript. 2) Simply grabbing every single feature ever invented from every language ever created and dumping them into one language is *not* how you design a good language. 3) You don't do original research in an industry standards body. Gradual typing is cool, but it is *still* an open research problem in 2010. You simply don't put an open research problem into the most widely used programming language on the planet.
Jörg W Mittag
*"It might have been an nice new language"* - It actually is, it's called ActionScript 3 ;)
poke
@Jörg: It seems that I've offended you!!!!!!!
codemaker
No, there will be a class-based version of ECMAScript. It's called ECMAScript 6. Also, much ECMAScript 4 is already implemented in ActionScript 3.
Eli Grey
And JavaScript 2.0 was never abandoned, it's still going to be made and implement ECMAScript 6.
Eli Grey
A: 

I think yes, there is something wrong. I would suggest to use it like this, you shall try using the work var instead of class, and add an '=' sign after the car as below.

var Car = {
var Make : String;
var Model : String;
var Year : Integer;
var Color : String;
var FullName : String;
function Car (make, model, year, color) {
this.Make = make;
this.Model = model;
this.Year = year;
this.Color = color;
this.FullName = this.Year + " " +
"<b>" + this.Make + "</b> " +
this.Model;
}
}
var mySUV = new Car("Toyota", "4Runner SR5",2001, "Thundercloud");
document.write ("I drive a " + mySUV.FullName);
Zain Shaikh
Is this way more efficient than using functions?
codemaker
That code is neither ECMAScript 4 or 6. That's a botched up object literal.
Eli Grey
+2  A: 

Just trying to merge things together, and clean up Zain's answer.

As Jörg W Mittag mentioned, ES4/JS2 is dead, so let's do it the ES3/JS1 way.

To compress Zain's answer:

/**
 * @constructor
 */
function Car(make, model, year, color) {
  this.Make = make;
  this.Model = model;
  this.Year = year;
  this.Color = color;
  this.FullName = this.Year + " " +
  "<b>" + this.Make + "</b> " +
  this.Model;
}
var mySUV = new Car("Toyota", "4Runner SR5",2001, "Thundercloud");
document.write ("I drive a " + mySUV.FullName);

If you wanted to do this without functions you could say:

var Car = {
  Make : String,
  Model : String,
  Year : Integer,
  Color : String,
  FullName : String
}

But then you would have to manually set the values, so no, there isn't really a good way of making a complex object without the use of a function.

PiPeep
`: String` was I think supposed to be ActionScript type notation, it doesn't exist in JavaScript.
bobince
Nope, I checked it in the closure compiler. I was amazed too.
PiPeep
JS2 can't be dead before it's even started. JS2 is going to be ES6.
Eli Grey
+2  A: 

The latest version of JavaScript is 1.8.1. If by JavaScript 2, you mean ECMAScript 6 (or 4), there's no engines that implement it yet. You can convert it to ECMA-262 code using Mascara.

To everyone else: Why are you people saying JavaScript 2 is dead? There were plans to implement ECMAScript 4 in JavaScript 2 and that has been abandoned. JavaScript 2 will most likely be an implementation of ECMAScript 6 but it isn't itself ECMAScript 6.

Eli Grey
A: 

Firefox 3.5 (which you said you are using in a comment) supports JavaScript 1.8.1, so any JS 2 features will not be supported.

David Dorward
+1  A: 

No mainstream browser supports the JavaScript 2.0 syntax today.

Mascara is a tool which will translate JavaScript 2.0 syntax (or something close to it) into ordinary JavaScript which will run in any browser, so you get classes, type-checking etc. but don't have to worry about compatibility.

Also, ActionScript 3 which is supported by Flash has a syntax close to JavaScript 2.

Note that JavaScript 2 is not an official standard, rather it is the Mozilla specific name for a proposed new version of JavaScript. The official vendor-neutral name for the JavaScript standard is ECMAScript.

The version of ECMAScript which is supported by mainstream browsers today is ECMAScript 3.

The proposed extensions, equivalent to what is called JavaScript 2.0 and Actionscript 3, was at one point called ECMAScript 4.

However the proposed ECMAScript 4 spec was felt by some vendors to be too ambitious, and it was decided to evolve the language iterative through less-ambitious steps. Hence the big ES4 spec was abandoned, and a new version called ECMAScript 5 which incorporated a small number of the features proposed for ES4 was created instead as a first step (notably, classes and type-annotations is not part of ES5). ES5 have been officially released, but is not yet fully supported by browsers. Work is ongoing on the next version, code named "Harmony", but which will probably end op being called ES6. It is yet unclear whether ES6 will include a syntax for classes.

The bottom line: you cannot run JavaScript 2 directly in any mainstream browser today, and it may take years before you are able to.

JacquesB