是否可以在通用级别覆盖“调用”函数,以便每次在应用程序中的任何地方调用一个方法时,都会发生一些事情。
我尝试覆盖 Object.call,但尽管我设法做到了,但它并没有改变我的应用程序的工作方式。
顺便说一句,即使它有效,我是否应该每次都显式调用“foo.call(this,args)”,或者正常的函数调用也将有效“foo(args)”?
最佳答案
听起来你想在这里做一些面向方面的编程....
JavaScript 作为一种 ECMAScript 方言,确实具有可调用对象 的概念。每个可调用对象都有一个名为 [[Call]] 的内部属性。该属性在第 5 版 ECMA-262 规范的第 8.6.2 节表 9 中进行了描述。它说:
Executes code associated with the object. Invoked via a function call expression. The arguments to the SpecOp are a this object and a list containing the arguments passed to the function call expression. Objects that implement this internal method are callable. Only callable objects that are host objects may return Reference values.
但要注意的是 [[Call]] 是一个内部属性,规范中说:
An internal property has no name and is not directly accessible via ECMAScript language operators. Internal properties exist purely for specification purposes.
所以你不能在你自己的 JavaScript 代码中加入这个机制。
现在
总而言之,最好不要在内置对象的原型(prototype)中使用预定义的标准方法。库和应用程序中的许多现有代码都依赖于 Function.prototype 中定义了两个方法,apply 和 call。确实,如果您更改 Function.prototype.call 的定义,那么如果您创建自己的函数 f,那么 f.call 将实际上(除非在 f 的原型(prototype)或 f 本身中被覆盖)执行该代码。如您所料,这不会通过直接调用 f 自动发生。您必须显式调用 call 方法。Function.prototype.call。不要惹它。当然,您可以通过多种方式实现一种 AOP 行为。一种是向 Function.prototype 添加一些其他方法,但也不要这样做。另一种是用before和after hooks编写自己的调用方法:function call(theThis, before, main, after, beforeArgs, mainArgs, afterArgs) {
before.apply(theThis, beforeArgs);
main.apply(theThis, mainArgs);
after.apply(theThis. afterArgs);
}
关于javascript - 是否可以覆盖 "call"函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7329923/
类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
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从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""-
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife