简体   繁体   English

使用browserify创建通用功能

[英]Create a common function with browserify

I'm pretty new to Browserify, I have more than a function that are used in different files. 我对Browserify还是很陌生,我拥有的不只是在不同文件中使用的功能。 I'm refactoring my code to extract this function in a single file, so I have to maintain this source for those functions. 我正在重构代码以将该功能提取到单个文件中,因此我必须维护这些功能的源代码。 These functions are already used in my other files so I don't want to change the way I call them, so if I was using a function in this way 这些函数已经在我的其他文件中使用,所以我不想更改调用它们的方式,所以如果我以这种方式使用函数

var x= foo();

I don't want to change the consumer javascript to something like 不想将消费者javascript 更改为类似

var x= lib.foo();

I created a file "./lib/common.js" 我创建了一个文件“ ./lib/common.js”

module.exports.trump = function (str, pattern) {
      var trumped = "";  // default return for invalid string and pattern

      if (str && str.length) {
        trumped = str;
        if (pattern && pattern.length) {
          var idx = str.indexOf(pattern);

          if (idx != -1) {
            trumped = str.substring(0, idx);
          }
        }
      }
      return (trumped);
        }

module.export.foo = function(options){
  return 1;
}

and in my app.js I have: 在我的app.js中,我有:

require('./lib/common.js')
trump(ui.item.PlaceId, "-sky" )

After browserifing my app.js file (no errors) I use it in my browser application, but I get Uncaught ReferenceError: trump is not defined 在浏览器中找到我的app.js文件(没有错误)之后,我在浏览器应用程序中使用了它,但是却出现了Uncaught ReferenceError: trump is not defined

How should I export my functions using a single common.js file to make them work when simply calling them like foo(); 当像foo();那样简单地调用函数时,如何使用单个common.js文件导出函数以使其工作foo(); ?

After requiring your library: 要求您的图书馆后:

var common = require('./lib/common.js');

you can merge it into this using: 你可以把它合并到this使用:

  1. jQuery extend() jQuery的extend()

     $.extend(this, common); 
  2. Underscore.js extend() Underscore.js extend()

     _.extend(this, common); 
  3. Object.assign() - ECMAScript 2015 (ES6) standard - Check browsers compatibility Object.assign()-ECMAScript 2015(ES6)标准-检查浏览器兼容性

     Object.assign(this, common) 
  4. An Object.assign polyfill (from https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign ): 一个Object.assign polyfill(来自https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign ):

     if (!Object.assign) { Object.defineProperty(Object, 'assign', { enumerable: false, configurable: true, writable: true, value: function(target) { 'use strict'; if (target === undefined || target === null) { throw new TypeError('Cannot convert first argument to object'); } var to = Object(target); for (var i = 1; i < arguments.length; i++) { var nextSource = arguments[i]; if (nextSource === undefined || nextSource === null) { continue; } nextSource = Object(nextSource); var keysArray = Object.keys(nextSource); for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) { var nextKey = keysArray[nextIndex]; var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); if (desc !== undefined && desc.enumerable) { to[nextKey] = nextSource[nextKey]; } } } return to; } }); } 

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

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