简体   繁体   English

如何在 javascript 模块模式中使用它?

[英]How to use this in javascript module pattern?

I'm studying module pattern and have a question.我正在学习模块模式并有一个问题。

I want to use 'settings' anywhere.我想在任何地方使用“设置”。


script1.js script1.js

var MODULE = (function() {
  var t = {};
  this.settings = { // this == Window
    users: ['john', 'emma']
  }
  return t;
})()

script2.js script2.js

MODULE.dom = function() {
  var t = this; // this == MODULE

  document.querySelector('p').textContent = t.settings.users[0]; // error

  function _say() {
    return t.settings.users[1] // error
  }

  return {
    say: _say
  }
}

MODULE.DOM = MODULE.dom.call(MODULE)

When use this.settings = {...} , 'this' means Window so code doesn't work.使用this.settings = {...}时, 'this'表示Window所以代码不起作用。

When use t.settings = {...} , 'this' means MODULE so code works but when write MODULE in dev console, settings is exposed in MODULE variable.使用t.settings = {...}时, “this”表示MODULE ,因此代码可以工作,但是在开发控制台中编写 MODULE 时,设置会暴露在 MODULE 变量中。 Is it ok?可以吗?

I'd greatly appreciate any help, thank you!我将不胜感激任何帮助,谢谢!

When use t.settings = {...} , 'this' means MODULE so code works使用t.settings = {...}时,'this' 表示 MODULE,因此代码有效

That's the right way to do it.这是正确的做法。

but when write MODULE in dev console, settings is exposed in MODULE variable.但是在开发控制台中编写 MODULE 时,设置会暴露在 MODULE 变量中。 Is it ok?可以吗?

It's mostly OK.大部分都没问题。

If you're worried about the client being able to type in the variable name and see the code that gets run - there's no way to avoid that.如果您担心客户端能够输入变量名并查看运行的代码 - 没有办法避免这种情况。 They can just look at the devtools to see what the network requests are, and what is downloaded - or look at the Sources panel.他们可以只查看 devtools 以查看网络请求是什么,以及下载了什么 - 或查看 Sources 面板。

If you're worried about running into naming collisions with larger scripts, then - sometimes, libraries deliberately assign themselves to the window to allow other parts of the code to access them.如果您担心遇到与较大脚本的命名冲突,那么 - 有时,库会故意将自己分配给 window 以允许代码的其他部分访问它们。 Perhaps you'd like your MODULE to be like this.也许你希望你的MODULE是这样的。 If not, then you should utilize JavaScript modules instead, which allow for scripts to be imported inside other scripts without polluting the global namespace at all or having any possibility of naming collisions.如果没有,那么您应该改用 JavaScript 模块,它允许在其他脚本中导入脚本,而不会污染全局命名空间或有任何命名冲突的可能性。 For example, you could do例如,你可以做

// script1.js
export const MODULE = {
  settings: {
    users: ['john', 'emma'];
  }
};
// script2.js
import { MODULE } from './script1.js';
// proceed to use MODULE

And you can do import { MODULE } from './script1.js';你可以import { MODULE } from './script1.js'; from any script file, not just script2.js .来自任何脚本文件,而不仅仅是script2.js

Personally, I consider the IIFE module pattern in JavaScript to be mostly obsolete nowadays.就个人而言,我认为 JavaScript 中的 IIFE 模块模式如今已基本过时。 For any reasonable-sized script, better to write code in separate files and then import and export as needed.对于任何大小合理的脚本,最好在单独的文件中编写代码,然后根据需要导入和导出。 (A 1000 line file is somewhat hard to debug and refactor. Ten 100 line files are easier to debug and refactor.) (一个 1000 行的文件有点难以调试和重构。十个 100 行的文件更容易调试和重构。)

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

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