Kamis, 30 Juli 2015

suka membantu: kamus biologi

suka membantu: kamus biologi:

  1. // A module_id (myModule) is used here for demonstration purposes only
  2.  
  3. define('myModule',
  4. ['foo', 'bar'],
  5. // module definition function
  6. // dependencies (foo and bar) are mapped to function parameters
  7. function ( foo, bar ) {
  8. // return a value that defines the module export
  9. // (i.e the functionality we want to expose for consumption)
  10. // create your module here
  11. var myModule = {
  12. doStuff:function(){
  13. console.log('Yay! Stuff');
  14. }
  15. }
  16.  
  17. return myModule;
  18. });
  19.  
  20. // An alternative example could be..
  21. define('myModule',
  22. ['math', 'graph'],
  23. function ( math, graph ) {
  24.  
  25. // Note that this is a slightly different pattern
  26. // With AMD, it's possible to define modules in a few
  27. // different ways due as it's relatively flexible with
  28. // certain aspects of the syntax
  29. return {
  30. plot: function(x, y){
  31. return graph.drawPie(math.randomGrid(x,y));
  32. }
  33. }
  34. };
  35. });
require on the other hand is typically used to load code in a top-level JavaScript file or within a module should you wish to dynamically fetch dependencies. An example of its usage is:

Understanding AMD: require()

  1. // Consider 'foo' and 'bar' are two external modules
  2. // In this example, the 'exports' from the two modules loaded are passed as
  3. // function arguments to the callback (foo and bar)
  4. // so that they can similarly be accessed
  5.  
  6. require(['foo', 'bar'], function ( foo, bar ) {
  7. // rest of your code here
  8. foo.doSomething();
  9. });

Dynamically-loaded Dependencies

  1.  
  2. define(function ( require ) {
  3. var isReady = false, foobar;
  4.  
  5. // note the inline require within our module definition
  6. require(['foo', 'bar'], function (foo, bar) {
  7. isReady = true;
  8. foobar = foo() + bar();
  9. });
  10.  
  11. // we can still return a module
  12. return {
  13. isReady: isReady,
  14. foobar: foobar
  15. };
  16. });
  17.  

Tidak ada komentar:

Posting Komentar