This time I will show you how to use seajs to write conventions in require, and what are the precautions for using seajs to write conventions in require. The following is a practical case. Let’s take a look. .
require Writing Conventions
When writing module code using Sea.js, you need to follow some simple rules.
It’s just a specification for writing and debugging! ! ! The built code does not need to follow the following conventions at all! ! ! ! ! !
1. Correct spelling
The first parameter of module factory Construction methodmust Name it require
.
// 错误! define(function(req) { // ... }); // 正确! define(function(require) { // ... });
2. Do not modify the
Do not rename the require
function, or rename the require
in any scope Assignment.
// 错误 - 重命名 "require"! var req = require, mod = req("./mod"); // 错误 - 重定义 "require"! require = function() {}; // 错误 - 重定义 "require" 为函数参数! function F(require) {} // 错误 - 在内嵌作用域内重定义了 "require"! function F() { var require = function() {}; }
3. Use direct quantities
require The parameter value of
must be a string direct quantity.
// 错误! require(myModule); // 错误! require("my-" + "module"); // 错误! require("MY-MODULE".toLowerCase()); // 正确! require("my-module");
When writing module code, you must follow these rules. In fact, just think of require
as a grammatical keyword.
About dynamic dependencies
Sometimes you hope to use require
for conditional loading:
if (todayIsWeekend) require("play"); else require("work");
I believe you have read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to use Koa2 to develop WeChat QR code scanning payment
How to use AngularJS to implement tab pages tab switching
The above is the detailed content of How to use seajs to write convention in require. For more information, please follow other related articles on the PHP Chinese website!