RequireJS - Defining Function (cách sử dụng require rất lạ) (ok)

https://www.tutorialspoint.com/requirejs/requirejs_defining_function.htm

Hàm define () có thể được sử dụng để tải các mô-đun (mô-đun có thể là một đối tượng, hàm, lớp hoặc một mã được thực thi sau khi tải một mô-đun). Bạn có thể tải các phiên bản khác nhau của cùng một mô-đun trong cùng một trang. Các phiên bản khác nhau có thể được phân tích theo cùng một thứ tự, ngay cả khi chúng được tải theo một thứ tự khác.

define(['module1', 'module2'], function(module1, module2) {
  //define the module value by returning a value
  return function() {};
});

C:\xampp\htdocs\php\index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script data-main = "libs/main" src = "libs/require.js"></script>
</head>
<body>
	<h1> RequireJS Sample Page </h1>
  <div id="content" class="navigation">
  	Lorem ipsum dolor sit amet consectetur adipisicing elit. Molestias, ut.
  </div>
</body>
</html>

C:\xampp\htdocs\php\libs\main.js

define(function (require) {
   var myteam = require("./team");
   var mylogger = require("./player");
   console.log("Player Name : " + myteam.player);
   mylogger.myfunc();
});

C:\xampp\htdocs\php\libs\team.js

define({
  player: "Lionel",
  team: "VietNam"
});

C:\xampp\htdocs\php\libs\player.js

define(function(require) {
  var myteam = require("./team");
  return {
    myfunc: function() {
      document.write("Name: " + myteam.player + ", Country: " + myteam.team);
    }
  };
});

Last updated

Navigation

Lionel

@Copyright 2023