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));
},
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 anew
statement) will invoke the class'sinitialize
method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With