Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining classes in JavaScript

Tags:

javascript

I have been reading up on object-oriented JavaScript. This is a link that I have been reading, Introduction to Object-Oriented JavaScript. However, I have also been reading the book Practical CakePHP Projects. I am trying to figure out why do they use class to define the class? From what I have read, you are supposed to define classes using the function keyword. The code below is an example from the book. Were the authors just wrong or is there something that I am missing?

var TravelMapprManager = Class.create({

    //Constructor
    initialize: function(map_container) {
        this.map_container = map_container;

        //Start the map.
        Event.observe(window, 'load', this.displayMap.bind(this));

        //Observe the map buttons.
        Event.observe(document, 'dom:loaded', this.initObservers.bind(this));
    },
like image 294
j.jerrod.taylor Avatar asked Feb 24 '23 15:02

j.jerrod.taylor


1 Answers

Class is not a keyword. It is a namespace for library code that contains the create function which sets up the constructor (the function you mentioned) and the prototype for you.

You can read more about it in the documentation for this particular library : http://api.prototypejs.org/language/Class/create/

Class.create creates a class and returns a constructor function for instances of the class. Calling the constructor function (typically as part of a new statement) will invoke the class's initialize method.

like image 135
Mike Samuel Avatar answered Feb 27 '23 06:02

Mike Samuel