6

what are the difference between these two ways of creating a class:

var apple = {
    type: "macintosh",
    color: "red",
    getInfo: function () {
        return this.color + ' ' + this.type + ' apple';
    }
}

function Apple (type) {
    this.type = type;
    this.color = "red";
    this.getInfo = function() {
        return this.color + ' ' + this.type + ' apple';
    };
}

and how do you instantiate and use the members?

1
  • You are only creating an object and not a class. Commented Jan 31, 2010 at 18:56

3 Answers 3

6

While JavaScript is an object-oriented language, it does not use classes. You don't create a "class" in JavaScript. You create a "prototype". JavaScript is considered a Prototype-based language.

The first example is known as "object-literal notation" for creating an object (a subset of which is popularly known as JSON). An analogy to this in class-based languages is a "static" class, in the sense that you don't need to create a new instance of an object in this case; It "just exists", once you define it. You wouldn't instantiate it, you'd access members of apple immediately since apple is already an object. It is also similar to creating an anonymous class in Java. You'd use it like this:

alert(apple.getInfo());

With the second example, you're creating a prototype (not a class), which can be used to instantiate objects of type Apple. You could use it like this:

var redDelicious = new Apple("Red Delicious");
alert(redDelicious.getInfo());

JavaScript allows you to modify and add to an object's prototype, so after you declared your Apple prototype, you could still continue to add or change things about it like this:

Apple.prototype.size = "7cm";

When you do this, all objects derived from the Apple prototype will gain a size field. This is the basis for how the PrototypeJS framework works to modify native JavaScript objects to add & fix functionality.

Keep in mind that it is considered bad practice to modify the prototype of native JavaScript objects, so you should avoid doing it whenever possible.

Sign up to request clarification or add additional context in comments.

6 Comments

First example is not a static class. It's a JS object created using object literal syntax.
@SolutionYogi I know it's not a static class; the first thing I mentioned in my answer is that there are no classes in JavaScript. However, it is easier to explain JavaScript's prototype-based syntax using analogies that people are already familiar with if they've programmed in a language that uses classes.
I still feel that calling it a 'static class' is completely wrong. Static class can't have state. I would put it this way: In traditional OOP languge, you can't create an object without class but in JavaScript, you can. And that's what the user did in the first example.
What's the similarity between object literal and "static" class? I found it quite strange when you call the first one "static" class, but say the second one "prototype (not a class)". If I'd make an analogy between JavaScript and other OOP language, I'd definitely say the second case is like creating the "normal" class (as in other OOP language), while the first one is just an object.
@SolutionYogi & @bryantsa I've updated my answer to more clearly explain what I was trying to explain in my answer. I agree that what I said was not entirely accurate. Although, static classes can have state, which is what every OO-programming language relies on when creating Singleton objects. And Java and C# (and others, I'm sure) can have an object without creating a class, though I think the compiler does create a class behind-the-scenes in IL code when you create an anonymous class.
|
3

Here are some differences:

  • The first way is incomplete, there is a semicolon missing after the last closing bracket.

  • The first way creates an object, while the second only declares a constructor that can be used to create objects.

  • The first way can only create a single object, while the second way can be used by the new keyword to create multiple objects.

  • The first way can't take any parameters to affect how the object is initialised, while the second can.

The first way creates a single object and assigns to the applevariable, which you can use to access the members:

alert(apple.type);

The second way is used with the new keyword to create instances:

var green = new Apple('Signe Tillisch');
alert(green.type);

Comments

3

Your first approach is JavaScript Object Literal and can be accessed by:

apple.type;  // returns "macintosh"
apple.getInfo(); // prints "red macintosh apple"

You can also add your own properties like wasGood or price like so:

apple.wasGood = "true";
apple.price = "0.50";  
alert(apple.price); // alerts "0.50"

Your second approach is instantiating an object and can be accessed by:

var myApple = new Apple("macintosh");
myApple.type; // returns "macintosh"
myApple.getInfo(); // returns "red macintosh apple"

Adding properties to your Apple object you need to use the prototype keyword:

Apple.prototype.price = "0.50";
Apple.prototype.wasGood = "true";
alert(myApple.price); // alerts "0.50"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.