与咖啡脚本的多个文件通信

2024-04-20前端开发问题
10

本文介绍了与咖啡脚本的多个文件通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

当我创建一个新的咖啡脚本文件时,我无法从另一个文件访问已编译代码中的代码,因为它被包装在某个函数范围内.例如:

When I create a new coffeescript file, I cannot access the code in the compiled code from another file because it gets wrapped in some function scope. For example:

CoffeeScript:

CoffeeScript:

class ChatService
  constructor: (@io) ->

生成的 Javascript:

Generated Javascript:

(function() {
  var ChatService;    
  ChatService = (function() {    
    function ChatService(io) {
      this.io = io;
    }    
    return ChatService;    
  })();    
}).call(this);

当试图在另一个文件中调用 ChatService 时,它没有被定义.如何使用 coffeescript 处理多个文件?

When trying to call ChatService in another file, it's not defined. How do I handle multiple files with coffeescript?

推荐答案

根据这是客户端代码还是服务器端代码,有两种略有不同的方法.

Depending on whether this is client- or server-side code, there are two slightly different approaches.

客户端:这里我们将跨文件可用的东西附加到全局命名空间(window),如下所示:

Client-side: Here we attach things that should be available across files to the global namespace (window) as follows:

class window.ChatService
  constructor: (@io) ->

然后,在另一个文件中,ChatServicewindow.ChatService 都将允许访问该类.

Then, in another file both ChatService and window.ChatService will allow access to the class.

Server-side:这里我们必须使用exportsrequire.在 ChatService.coffee 文件中,您将拥有以下内容:

Server-side: Here we must use exports and require. In the ChatService.coffee file, you would have the following:

class exports.ChatService
  constructor: (@io) ->

然后,要从另一个文件中获取它,您可以使用:

Then, to get at it from another file you can use:

ChatService = require('ChatService.coffee').ChatService

注意:如果您从 ChatService.coffee 获得多个类,这是 CoffeeScript 的 dict 解包真正大放异彩的地方,例如:

Note: If there are multiple classes that you are getting from ChatService.coffee, this is one place where CoffeeScript's dict unpacking really shines, such as:

{ChatService, OtherService} = require('ChatService.coffee')

<小时>

两者:基本上,我们根据所处的环境选择是运行服务器端代码还是客户端代码.一种常见的方法:


Both: Basically, we choose whether to run server-side or client-side code based on which environment we're in. A common way to do it:

class ChatService
  constructor: (@io) ->

if typeof module != "undefined" && module.exports
  #On a server
  exports.ChatService = ChatService
else
  #On a client
  window.ChatService = ChatService

得到它:

if typeof module != "undefined" && module.exports
  #On a server
  ChatService = require("ChatService.coffee").ChatService
else
  #On a client
  ChatService = window.ChatService

可以跳过第二个块的 else 子句,因为 ChatService 已经引用了附加到 window 的引用.

The else clause of the second block can be skipped, since ChatService already refers to the reference attached to window.

如果你要在这个文件中定义很多类,那么定义它们可能更容易:

If you're going to define a lot of classes in this file, it may be easier to define them like:

self = {}

class self.ChatService

然后像 module.exports = self 在服务器上附加它们,在客户端附加 _.extend(window, self) (替换 _.extend 与另一个 extend 函数(视情况而定).

And then attach them like module.exports = self on the server and _.extend(window, self) on the client (replace _.extend with another extend function as appropriate).

这篇关于与咖啡脚本的多个文件通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5