Undefined error when using javascript objects.

It is better to explain the code, is there a way for this error? Im trying to simulate a namespace.

window.SomeNamespace = { Notification: Backbone.Model.extend(), Notifications: Backbone.Collection.extend({ model: SomeNamespace.Notification //error here. SomeNamespace is not defined }), }; 
+4
source share
1 answer

window.SomeNamespace , and therefore the global SomeNamespace will not be defined until the right side = is executed. Therefore, you will have to break it into two parts.

 window.SomeNamespace = { Notification: Backbone.Model.extend(), }; window.SomeNamespace.Notifications = Backbone.Collection.extend({ model: SomeNamespace.Notification }); 

Of course, you can do it better by using the extend() method.

+5
source

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


All Articles