Defining classes in JavaScript

I read about object oriented JavaScript. This is the link I read, Introduction to Object Oriented JavaScript . However, I also read CakePHP Practical Projects book. I'm trying to understand why they use a class to define a class? From what I read, you should define classes using the function keyword. The code below is an example from a book. Were the authors wrong or is there something 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)); }, 
+4
source share
2 answers

Class not a keyword. This is the namespace for the library code that contains the create function, which sets the constructor (specified function) and prototype for you.

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

Class.create creates a class and returns a constructor for instances of the class. A call to the constructor function (usually as part of the new operator) will call the method of the initialize class.

+7
source

I wonder if they use some kind of library in the book you are reading? This may answer your direct question.

Below is a very quick example (I hope it’s correct, since I have not tested it, but it should give you an idea) of what you are trying to do (just found this, which can be useful: http://www.phpied.com/3 -ways-to-define-a-javascript-class )

 function TravelMapprManager() { } TravelMapprManager.prototype.initialize = function(mapContainer) { this.mapContainer = mapContainer; ... }; var mappr = new TravelMapprManager(); mappr.initialize(...); 
+1
source

Source: https://habr.com/ru/post/1346157/


All Articles