简体   繁体   English

我想知道这种在javascript中编写可重用代码的方法

[英]I would like to know this approach of writing reusable code in javascript

I was looking into a tutorial sometime ago and in that tutorial the author was using following approach to write reusable code. 我前一段时间正在研究一个教程,在那个教程中,作者正在使用以下方法编写可重用的代码。 I am trying to find that tutorial but no avail yet. 我正在尝试查找该教程,但无济于事。 I would like to know a little about following approach and how can I reuse this with new operator without adding the module to global namespace? 我想对以下方法有所了解,如何在不将模块添加到全局名称空间的情况下与new操作符一起重用?

(function(factory) {

})(function() {
    // All of the logic code was here in that tutorial.
});

I think you should check out the module pattern which is aimed at creating reusable well encapsulated javascript components 我认为您应该查看旨在创建可重用且封装良好的javascript组件的模块模式

here is a short example 这是一个简短的例子

 MyModule = function(){
      var privateAttribute = 2;
      return {
         publicAttribute : "this is a public attribute";
         publichMethod : function (param1){console.log(param1);}
      }

 }();

var module = MyModule;
module.putlicMethod("hello world");

So, the idea is that the second function is what you are passing in as the factory parameter in the first function, so you would create instances of factory and write code against those instances in the TOP function - not the bottom function. 因此,我们的想法是第二个函数是您在第一个函数中作为factory参数传入的内容,因此您将创建factory实例,并针对这些实例在TOP函数中而不是底部函数中编写代码。 The bottom function is your module definition. 底部函数是您的模块定义。

Thanks to @Bergi for getting me started on the right track. 感谢@Bergi使我正确地开始了。

Here's an example with code: 这是带有代码的示例:

 (function(factory) { var constructor = new factory(); var obj1 = new constructor(); var obj2 = new constructor(); console.log("obj1: ", obj1); console.log("obj2: ", obj2); })(function() { return function() { return { prop1: "123", prop2: "ABC" } } }); 

From your code example it is difficult to know exactly what you are looking for, but the code looks similar to the Universal Module Definition pattern (UMD) used to define reusable modules for use with various module loaders. 从您的代码示例中很难确切地知道您要查找的内容,但是该代码看起来与用于定义可重用模块以与各种模块加载器一起使用的通用模块定义模式(UMD)相似。

An example of UMD ( taken from this page ) which illustrates what you describe in your code—the actual code logic (the module) in the lower function, which is passed as the argument factory to the upper function which handles the module setup: UMD的一个示例( 从此页面获取 )说明了您在代码中描述的内容-下部函数中的实际代码逻辑(模块),该参数作为参数factory传递给处理模块设置的上部函数:

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD
        define(['jquery'], factory);
    } else if (typeof exports === 'object') {
        // Node, CommonJS-like
        module.exports = factory(require('jquery'));
    } else {
        // Browser globals (root is window)
        root.returnExports = factory(root.jQuery);
    }
}(this, function ($) {
    //    methods
    function myFunc(){};

    //    exposed public method
    return myFunc;
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM