jjzjj

module-export

全部标签

ruby - class_eval、class_exec、module_eval 和 module_exec 之间有什么区别?

我正在阅读Module文档,但似乎无法理解它们之间的差异以及应该在何处使用。eval与exec有何不同? 最佳答案 我将通过在您的问题中包含instance_{eval|exec}来回答您的问题。{instance|module|class}_{eval|exec}的所有变体都会更改当前上下文,即self的值:classArraypself#prints"Array"43.instance_eval{pself}#prints"43"end现在说说区别。eval版本接受字符串或block,而exec版本只接受block但允许您向其传

ruby-on-rails - rails : I can't call a function in a module in/lib - what am I doing wrong?

我有一个模块保存在/lib中作为test_functions.rb看起来像这样moduleTestFunctionsdefabcputs123endend进入ruby​​脚本/运行程序,我可以看到该模块正在自动加载(良好的配置约定等等......)>>TestFunctions.instance_methods=>["abc"]所以方法是已知的,让我们尝试调用它>>TestFunctions.abcNoMethodError:undefinedmethod`abc'forTestFunctions:Modulefrom(irb):3没有。这个怎么样?>>TestFunctions::a

ruby module_function 与包含模块

在ruby​​中,我知道可以使用module_function在模块中混合使用模块函数,如此处所示。我知道这是多么有用,因此您可以在不混入模块的情况下使用该函数。moduleMyModuledefdo_somethingputs"helloworld"endmodule_function:do_somethingend我的问题是为什么您可能希望以这两种方式定义函数。为什么不拥有defMyModule.do_something或defdo_something在什么样的情况下,将函数混入或用作静态方法会有用? 最佳答案 想到Enumer

ruby-on-rails - rails : How do I write tests for a ruby module?

我想知道如何为混合到几个类中的模块编写单元测试,但不太知道如何去做:我是通过在其中一个测试文件中为包含它们的类编写测试来测试实例方法(似乎不正确),还是您能以某种方式将包含方法的测试保存在特定的单独文件中到模块?同样的问题也适用于类方法。我应该像普通Rails模型那样为模块中的每个类创建一个单独的测试文件,还是它们存在于通用模块测试文件中(如果存在)? 最佳答案 恕我直言,您应该进行涵盖模块所有用途的功能测试覆盖率,然后在单元测试中对其进行隔离测试:setupdo@object=Object.new@object.extend(Gr

javascript - 使用 `module` 作为命名空间

我一直在我的node.js模块中使用一种模式,这种模式对我来说非常明显,以至于我认为它一定有问题,否则我会看到更多人这样做。为了保留模块全局的私有(private)变量,我只是将它们作为属性附加到模块对象上。像这样:module.exports={init:function(){module.someClient=initializeSomethingHere()},someMethod:function(done){module.someClient.doSomething(done)}}这对我来说似乎比这样的东西更可取......varsomeClient;module.expor

javascript - 减少用于生产的 node_modules 的大小

我们的部署过程需要很长时间,部分原因是将node_modules文件夹传递到生产服务器。我的Package.json看起来像这样:{"name":"coms-sass","version":"0.0.1","description":"SassgulptaskforCOMSServicePortal","main":"gulpfile.js","dependencies":{"angular":"^1.5.5","angular-sanitize":"=1.5.5","angular-ui-bootstrap":"^1.3.2","gridster":"^0.5.6","gulp":"

javascript - 为什么我们需要将 module.exports 作为参数传递,因为我们已经将 module 作为参数传递了?

我一直在阅读一些关于Node.js的在线教程。我的理解是,在使用require(./file-path)函数时,Node获取该文件的内容并包装在一个立即调用的函数中(function(exports,require,module,__filename,__dirname){//content}())我了解exports和module.exports之间的区别。这就是我在互联网上搜索上述问题时所能看到的全部内容。但我的问题是,为什么我们需要将module.exports和module传递给包装IIFE?我们可以单独传递模块,然后从中获取module.exports。这样做有什么好处吗?通

javascript - react : Flow: Import a local js file - cannot resolve issue module

我正在使用Flow:Statictypecheckinglibrary对于React前端应用程序,它会为从src目录的内部导入抛出“无法解析”:Exampleinfileatpath:src/abc/def/ghi/hello.jsx,Iamusingthefollowingimport:importwordsfrom'../words';-->Throwserror"Cannotresolvemodule../wordswords.jsisinsrc/abc/defdir已编辑:安装flow-typed后,我的.flowconfig看起来像这样:[ignore].*/node_mod

javascript - 错误 : "Could not find a declaration file for module ' react-search-input'"

我正在尝试安装react-input-search。我有错误:Couldnotfindadeclarationfileformodule'react-search-input'.'.../app/node_modules/react-search-input/lib/index.js'implicitlyhasan'any'type.Trynpminstall@types/react-search-inputifitexistsoraddanewdeclaration(.d.ts)filecontainingdeclaremodule'react-search-input';ts(70

javascript - karma : Can't find variable: exports

我写了一个可以同时用于后端和客户端的Node模块(exports||window).Bar=(function(){returnfunction(){....}})();现在我的karma测试使用PhantomJs并提示不存在的exports变量gulp.task('test',function(){varkarma=require('karma').server;karma.start({autoWatch:false,browsers:['PhantomJS'],coverageReporter:{type:'lcovonly'},frameworks:['jasmine'],fi