Using dojo.require () without dojo.declare ()

I am pretty confused from the Dojo documentation. How can I use dojo.require () without using dojo.declare ()? The reason I don't want to use dojo.declare () is because it declares a declared global variable class.

Now my code is as follows:

HTML file: dojo.require('module.test'); Module/test.js: dojo.provide('module.test'); function test() { return 'found me'; } 

I just can't get Dojo to return the test () method anywhere. What is the correct template to use dojo.require () without an declaration?

+4
source share
3 answers

I think you confuse dojo.provide / dojo.require with dojo.declare . These are completely different concepts.

Things related to modules

dojo.provide defines a module.

dojo.require requires the module to be defined before running any code later.

Things related to JavaScript classes

dojo.declare is something completely different. It declares a Dojo-style class.

You can have several classes in a module or several modules that make up one class. In general, the modules! == classes, and they are completely unconnected by concepts.

+6
source

dojo.provide defines a code module so that the loader sees it and creates an object from the global namespace with this name. From this point you can bind the code directly to this global variable, for example.

Change houses / test.js:

 dojo.provide('module.test'); module.test.myFunc = function() { return 'found me'; } 

There are various templates that you can use, for example, creating a closure and hiding implementations of "private" functions by exposing them through a global link created in dojo.provide:

 dojo.provide('module.test'); function(){ // closure to keep top-level definitions out of the global scope var myString = 'found me'; function privateHelper() { return myString; } module.test.myFunc = function() { return privateHelper(); } }(); 

Note that the above simply puts the methods directly on the object. Now there is also dojo.declare , which is often used with dojo.provide to create an object with prototypes and mixins, so that you can create instances with the keyword "new" with some inheritance, even simulating multiple vai mixins inheritance. Such an abstraction of OO is often abused. It approximates the patterns required by languages ​​such as Java, so some people use it to declare objects for everything.

Note that with Dojo 1.5, dojo.declare returns an object and does not have to declare anything in the global scope.

+2
source

Here is a template that I sometimes use (this will be the contents of test.js):

 (function() { var thisModule = dojo.provide("module.test"); dojo.mixin(thisModule, { test: function() { return "found me"; } }); })(); 

You can now reference module.test.test() on your HTML page.

+1
source

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


All Articles