简体   繁体   English

将* .js文件的内容放入对象

[英]Placing the contents of a *.js file into an Object

I have a javascript file with a lot of global objects. 我有一个包含许多全局对象的javascript文件。 That is, I have a lot of the following sorts of code going on: 也就是说,我正在执行以下许多代码:

var foo = 5;
var bar = "string";

var getStuff = function () {
    //...getStuff
}

Question: What is the easiest way for me to place the entirety of this file into an object? 问题:对我来说,将这个文件的全部放入对象的最简单方法是什么?

It seems to me my options are as follows: 在我看来,我的选择如下:

  1. Wrap the entire file in a var Object = { ... }; 将整个文件包装在var Object = { ... }; . The problem is that is that I have to change the syntax of how variables and functions are declared. 问题是我必须更改声明变量和函数的语法。

  2. Manually prefix Object.xxx before each declaration. 在每个声明之前手动为Object.xxx前缀。 This is also a lot of manual work, and makes things slightly verbose. 这也是很多手工工作,使事情变得有些冗长。

Is there a better way? 有没有更好的办法? Or if not, is there a reason to prefer (1) or (2)? 否则,是否有理由偏爱(1)或(2)?

I guess you want this way 我想你想这样

var someObject = function (window, document, undefined) {
  /*you code...etc here*/
  return {
    method1 : your_method...,
    method2 : your_method...,
    method2 : your_method...,
    object : your_object...
  }
} (window, document);

The reason to pass window and document here is to make it be a local variable instead a foreign variable to minimize the cost for lookup variable. 在此处传递窗口和文档的原因是使它成为局部变量而不是外部变量,以最大程度地减少查找变量的开销。 Most js engine now can handle that by themself, so this may not be really required nowadays. 现在,大多数js引擎都可以自行处理,因此现在可能并不需要。 Just in case this code may be ran on a really old browser. 以防万一此代码可能在旧版浏览器上运行。

And the undefined passed that way is also due to compatibility reason, undefined is a override variable on some old browsers. 并且以这种方式传递的undefined也是出于兼容性原因,在某些旧版浏览器中undefined是覆盖变量。 New browsers won't allow such action. 新的浏览器将不允许此类操作。

You're not looking for a Class or Object. 您不是在寻找类或对象。 Looking at your description, I think you want something like a module. 查看您的描述,我认为您需要类似模块的东西。

You can hide your variables from global scope and "export" relevant bits like this: 您可以在全局范围内隐藏变量,并“导出”相关位,如下所示:

var MyModule = (function(){

  // regular code here
  var hidden = 10,
      visible = 20;

  function doSomething() {
    return hidden + visible;
  }

  // the "export" part
  return {
    twenty: visible,
    doSomething: doSomething
  };

})(); 

// then:

MyModule.doSomething(); // => 30
MyModule.twenty + 1 // => 21
MyModule.hidden     // undefined - no such thing here 
// hidden, visible and doSomething without MyModule. are invisible here

That's one example of emulating modules in JS. 那是在JS中模拟模块的一个示例。 That (function(){...})() part is called IIFE . (function(){...})()部分称为IIFE

There are different JavaScript design patterns you can follow to accomplish this. 您可以遵循不同的JavaScript设计模式来完成此任务。

I suggest going over this online book: http://addyosmani.com/resources/essentialjsdesignpatterns/book/ 我建议浏览一下此在线书: http : //addyosmani.com/resources/essentialjsdesignpatterns/book/

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

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