RequireJS:如何定义包含单个“类”的模块?

发布于 2024-10-15 13:01:44 字数 821 浏览 8 评论 0原文

我有许多 JavaScript“类”,每个类都在自己的 JavaScript 文件中实现。对于开发,这些文件是单独加载的,对于生产,它们是串联的,但在这两种情况下,我都必须手动定义加载顺序,确保如果 B 使用 A,则 B 在 A 之后。我计划使用 RequireJS 作为 CommonJS Modules/AsynchronousDefinition 自动为我解决这个问题。

有没有比定义每个导出一个类的模块更好的方法?如果没有,如何命名模块导出的内容?导出类“Employee”的模块“employee”(如下例所示)不会让人感觉DRY 对我来说足够了。

define("employee", ["exports"], function(exports) {
    exports.Employee = function(first, last) {
        this.first = first;
        this.last = last;
    };
});

define("main", ["employee"], function (employee) {
    var john = new employee.Employee("John", "Smith");
});

I have a number of JavaScript "classes" each implemented in its own JavaScript file. For development those files are loaded individually, and for production they are concatenated, but in both cases I have to manually define a loading order, making sure that B comes after A if B uses A. I am planning to use RequireJS as an implementation of CommonJS Modules/AsynchronousDefinition to solve this problem for me automatically.

Is there a better way to do this than to define modules that each export one class? If not, how to you name what the module exports? A module "employee" exporting a class "Employee", as in the example below, doesn't feel DRY enough to me.

define("employee", ["exports"], function(exports) {
    exports.Employee = function(first, last) {
        this.first = first;
        this.last = last;
    };
});

define("main", ["employee"], function (employee) {
    var john = new employee.Employee("John", "Smith");
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薆情海 2024-10-22 13:01:44

AMD 提案 允许您仅返回导出对象的值。但请注意,这是 AMD 提案的一个功能,它只是一个 API 提案,并且会使将该模块转换回常规 CommonJS 模块变得更加困难。我认为这没关系,但了解有用的信息。

因此,您可以执行以下操作:

我更喜欢导出构造函数的模块以大写名称开头,因此该模块的非优化版本也将位于 Employee.js

define("Employee", function () {
    //You can name this function here,
    //which can help in debuggers but
    //has no impact on the module name.
    return function Employee(first, last) {
        this.first = first; 
        this.last = last;
    };
});

现在,在另一个模块中,您可以使用 Employee像这样的模块:

define("main", ["Employee"], function (Employee) {
    var john = new Employee("John", "Smith");
});

The AMD proposal allows you to just return a value for the exported object. But note that is a feature of the AMD proposal, it is just an API proposal, and will make it harder to translate the module back to a regular CommonJS module. I think that is OK, but useful info to know.

So you can do the following:

I prefer modules that export a constructor function to start with an upper-case name, so the non-optimized version of this module would also be in Employee.js

define("Employee", function () {
    //You can name this function here,
    //which can help in debuggers but
    //has no impact on the module name.
    return function Employee(first, last) {
        this.first = first; 
        this.last = last;
    };
});

Now in another module, you can use the Employee module like so:

define("main", ["Employee"], function (Employee) {
    var john = new Employee("John", "Smith");
});
奢华的一滴泪 2024-10-22 13:01:44

作为 jrburke 答案的补充,请注意您不必直接返回构造函数。对于大多数有用的类,您还需要通过原型添加方法,您可以这样做:

define('Employee', function() {
    // Start with the constructor
    function Employee(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    // Now add methods
    Employee.prototype.fullName = function() {
        return this.firstName + ' ' + this.lastName;
    };

    // etc.

    // And now return the constructor function
    return Employee;
});

事实上,这正是 此示例位于 requirejs.org

As an addition to jrburke's answer, note that you don't have to return the constructor function directly. For most useful classes you'll also want to add methods via the prototype, which you can do like this:

define('Employee', function() {
    // Start with the constructor
    function Employee(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    // Now add methods
    Employee.prototype.fullName = function() {
        return this.firstName + ' ' + this.lastName;
    };

    // etc.

    // And now return the constructor function
    return Employee;
});

In fact this is exactly the pattern shown in this example at requirejs.org.

苹果你个爱泡泡 2024-10-22 13:01:44

更新方法2023

define(function(){  
    return class Fullname{
        constructor(firstname, lastname){
            this.firstname = firstname
            this.lastname = lastname
        }
        
        generate() {
            return this.firstname+" "+this.lastname
        }
    }   
});

用法:

define(['your/modules/path'], function(Fullname){
    let name = (new Fullname('Giang', 'Imgs')).generate();
    console.log(name)
}   

Update method 2023

define(function(){  
    return class Fullname{
        constructor(firstname, lastname){
            this.firstname = firstname
            this.lastname = lastname
        }
        
        generate() {
            return this.firstname+" "+this.lastname
        }
    }   
});

Usage:

define(['your/modules/path'], function(Fullname){
    let name = (new Fullname('Giang', 'Imgs')).generate();
    console.log(name)
}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文