我遇到了一个有趣的问题,至少我认为它很有趣,而且有点烦人。我有一个类,对于这个问题,我将使其非常简单......
class Foo {
static pageChange() {
console.log('The page changed');
}
}
现在,我可以使用 Foo.pageChange() 访问它,没问题,一切都按预期进行。当我尝试动态访问它时,困难的部分和有趣的部分出现了。 我有一个单独的对象来监视事件并根据需要处理它们的调度。这与 Google 可视化库有关,我在其中有一个表,该表有与之相关的事件。我有一个对象负责从 PHP 输出创建所有这些。这是一个非常棒的系统,在一个简单的解释中,您可以在 PHP 中执行类似的操作...
GoogleVisLibrary::renderChart(
array(
'chartType' => 'table',
'chartData' => $this->chartData,
'chartOptions' => $this-chartOptions,
'events' => array(
'sort' => 'Foo.pageChange'
)
);
现在将创建 teh 表和所有好东西。问题是在 javascript 的 Foo 类中访问该静态方法。在将 Foo 创建为一个类之前,我拥有的是这个。
var Foo = {
pageChange: function() {
console.log('page changed');
}
}
然后在我的事件库处理程序中它看起来像这样..
for(var i = 0, l = events.length; i < l; i++) {
window[events.className][events.fnName].apply();
}
那会很好用,因为可以通过 window['Foo'] 访问 Foo 但是当您使用第一个代码片段中显示的类定义时,您不能再从 super 全局窗口访问它,它只会输出“未定义”。
那么,有没有办法像通过全局窗口访问 Foo 对象那样,通过动态引用访问类中的静态方法?
我希望这是有道理的,我正在正确地解释它。如果有任何不明白的地方,请随时提问,我会尽力解释得更好。预先感谢您提供的任何帮助。
最佳答案
要获得对 window 对象的引用,您需要明确地执行此操作:
window.Foo = class Foo { ... }
在 this answer 中阅读更多关于不是 window 对象属性的类的信息,其中还引用了 ECMA2015 Specification, Section 8.1.1.4: Global Environment Records :
A global Environment Record is logically a single record but it is specified as a composite encapsulating an object Environment Record and a declarative Environment Record. The object Environment Record has as its base object the global object of the associated Realm. This global object is the value returned by the global Environment Record’s GetThisBinding concrete method. (E.g., the global object referenced by
windowon browsers — T.J.) The object Environment Record component of a global Environment Record contains the bindings for all built-in globals (clause 18) and all bindings introduced by a FunctionDeclaration, GeneratorDeclaration, or VariableStatement contained in global code. The bindings for all other ECMAScript declarations in global code are contained in the declarative Environment Record component of the global Environment Record.
最好不要为此使用全局对象,而是使用一个特定的对象来包含您的类,并使您的事件管理库基于该对象,如以下简化片段所示:
(function () {
var classes = {
Foo: class Foo {
static pageChange() {
console.log('The page changed');
}
}
}
/////////////
var events = [{
className: 'Foo',
fnName: 'pageChange'
}];
for(var event of events) {
classes[event.className][event.fnName].apply();
}
}());
关于全局窗口中无法访问 Javascript ES6 类定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37711603/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r
我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun