Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does jQuery or JavaScript have the concept of classes and objects?

Tags:

I found the following code somewhere, but I am not understanding the code properly.

ArticleVote.submitVote('no');return false;

Is ArticleVote a class and submitVote() a function of that class?

Or what does the above code mean? And is there any concept of classes and objects in jQuery or in traditional JavaScript? How to create them? Please share some reference links or code.

like image 332
djmzfKnm Avatar asked Jul 02 '09 11:07

djmzfKnm


People also ask

Is there a concept of class in JavaScript?

A JavaScript class is a blueprint for creating objects. A class encapsulates data and functions that manipulate data. Unlike other programming languages such as Java and C#, JavaScript classes are syntactic sugar over the prototypal inheritance. In other words, ES6 classes are just special functions.

Are classes objects in JavaScript?

A JavaScript class is not an object. It is a template for JavaScript objects.

Is class and object same in JavaScript?

Object is the base type in JavaScript i.e. all the user defined data types inherits from Object in one way or another. So if you define a function or a class [note as of now JS doesn't support class construct, but its proposed in ECMAScript version 6], it will implicitly inherit from Object type.

Can we create class and object in JavaScript?

Javascript does not use class-based inheritance. So, you can't make classes in Javascript, except to emulate them using workarounds that are IMHO clumsy and complicated. Javascript is a prototypal language. Using the new keyword creates a new object based on the prototype property of the constructor object.


1 Answers

Everything is an object in JavaScript

As opposed to other purportedly pure OOP languages. Functions are objects too, but they may just as well be constructor of objects.

var ObjectCreator = function () {
};

The above is a function which if called appropriately, creates an object. Called appropriately means that you have to use the new operator:

var obj = new ObjectCreator;

So while JavaScript does not have classes per se, has means to emulate that behavior. For example:

class Foo {
    public void bar() {}
}

Foo foo = new Foo();

is equivalent to the following JS code:

var Foo = function () {
    // constructor
};

Foo.prototype.bar = function () {}

var foo = new Foo;

Inheritance is different

The real difference comes when you want to use inheritance, which is a different type of inheritance (prototypal). So, given two pseudo-classes Foo and Bar, if we want Bar to extend from Foo, we would have to write:

var Foo = function () {};
var Bar = function () {};

Bar.prototype = new Foo; // this is the inheritance phase

var bar = new Bar;

alert(bar instanceof Foo);

Object literals

While constructor functions are useful, there are times when we only need only one instance of that object. Writing a constructor function and then populate its prototype with properties and methods is somehow tedious. So JavaScript has object literals, which are some kind of hash tables, only that they're self-conscious. By self-conscious I mean that they know about the this keyword. Object literals are a great way to implement the Singleton pattern.

var john = {
    age : 24,

    isAdult : function () {
        return this.age > 17;
    }
};

The above, using a constructor function would be equivalent to the following:

var Person = function (age) {
    this.age = age;
};

Person.prototype.isAdult = function () {
    return this.age > 17;
};

var john = new Person(24);

What about that prototype thingy

As many have said, in JavaScript objects inherit from objects. This thing has useful aspects, one of which may be called, parasitic inheritance (if I remember correctly the context in which Douglas Crockford mentioned this). Anyway, this prototype concept is associated with the concept of prototype chain which is similar to the parent -> child chain in classical OO languages. So, the inheritance stuff. If a bar method is called on a foo object, but that object does not have a bar method, a member lookup phase is started:

var Baz = function () {};

Baz.prototype.bar = function () {
    alert(1);
};

var Foo = function () {};
Foo.prototype = new Baz;

var foo = new Foo;

/*
 * Does foo.bar exist?
 *      - yes. Then execute it
 *      - no
 *          Does the prototype object of the constructor function have a bar
 *          property?
 *              - yes. Then execute it
 *              - no
 *                  Is there a constructor function for the prototype object of
 *                  the initial construct function? (in our case this is Baz)
 *                      - yes. Then it must have a prototype. Lookup a bar
 *                        member in that prototype object.
 *                      - no. OK, we're giving up. Throw an error.
 */
foo.bar();

Hold on, you said something about parasitic inheritance

There is a key difference between classical OO inheritance and prototype-based inheritance. When objects inherit from objects, they also inherit state. Take this example:

var Person = function (smart) {
    this.smart = smart;
};

var Adult = function (age) {
    this.age = age;
};

Adult.prototype = new Person(true);

var john = new Adult(24);

alert(john.smart);

We could say that john is a parasite of an anonymous Person, because it merciless sucks the person intelligence. Also, given the above definition, all future adults will be smart, which unfortunately is not always true. But that doesn't mean object inheritance is a bad thing. Is just a tool, like anything else. We must use it as we see fit.

In classical OO inheritance we can't do the above. We could emulate it using static fields though. But that would make all instances of that class having the same value for that field.

like image 89
Ionuț G. Stan Avatar answered Oct 21 '22 10:10

Ionuț G. Stan