Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
FREELessons:16Length:1.8 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.2 Exporting Values From Modules

For a module to be consumed by other modules, it needs to return a value of some kind. This can take a variety of different formats, so let's take a look at the different options we have.

2.2 Exporting Values From Modules

Hi, folks. In this lesson we'll see the different ways we have of returning a value from a module. We can also see what types of value we can return too. One of the limitations of CommonJS is that it can be awkward to return a single function. To return a value from a CommonJS module you need to use the exports object. RequireJS can use an exports object as well. But it can also return a plain function if necessary. This will be useful for our task and list modules, which are essentially just constructors. Both of these modules have constructors inside them, so we can return these as pure constructors of we want. The task manager requires the task module. So to make the task constructor usable inside the task manager, we can just return the constructor from inside the task module. [BLANK_AUDIO] Now the task module is returning a value. In this case, the task constructor. We could also add the same to the list module. [BLANK_AUDIO] In the task manager, we can add a method to create a new task by making use of the constructor. [BLANK_AUDIO] So, we've got a simple wrapper for the task constructor that returns a new task instance. The task manager module itself doesn't contain a constructor and doesn't need to return a constructor. Instead, we want to return an API that can be used elsewhere in the application. We'll add other methods later. For now, let's just have the task manager return an object that exposes the createTask method. [BLANK_AUDIO] Great. So now. How any of the modules that require the task manager can use the createTask method. Both of these ways of returning your value are down to the flexibility of AMD. As I mentioned earlier, with CommonJS we have to make use of an exports object to export values, a bit like how we return an object from our task manager. With AMD we can also make use of an exports object, and in some cases, it makes sense to do so. Like here, for example. The task manager still has to create an object literal to return. So let's make use of the exports object instead. Any module we define will automatically be passed to special object called exports. In this case because we're using the AMD style dependency array we can exports as if it were also a module. Now inside the module we can attach properties or methods to the exports object. [BLANK_AUDIO] This is all we need to do to make use of exports. If we use it it will automatically be returned from the module without us needing to specifically return it. In the last lesson we saw how we can define module dependencies as an array in the AMD style or by using the require method in the Simplified CommonJS wrapper style. We can also make use of the exports object when we're using the Simplified CommonJS wrapper style. In this case the exports object is passed to the callback function in the module definition directly after the require method. So in this lesson, we looked at the different ways of returning a value from a module. We saw that AMD is extremely flexible in that the return value from a module can be any value that we can return from a regular JavaScript function. We can return objects, or when using constructors, we can return functions. We also saw how we can make use of the CommonJS style exports objects either with AMD array dependency injection style or with the Simplified CommonJS wrapper style. At the moment, however, we still can't actually use our modules in any way because we don't have an entry point to our application. Don't worry though, this is exactly we'll cover in the next lesson. Thanks for watching.

Back to the top