我已经四处搜索,但到目前为止还没有找到重复项,我可能使用了错误的关键字...
我正在尝试临时更改存储在对象中的函数,但无法将其设置回之前的状态。
考虑一下:
// Set the options object
var options = {
success: function(){
console.log('Original Function Called');
}
}
// Save the options
$('#foo').data('bar',options);
然后在另一个函数中:
// Get the options
var options = $('#foo').data('bar');
// Store the old options
var old_options = options;
// Temporarily change the success function
options.success = function(){
console.log('Temporary Function Called');
}
// Save the options
// This allows the other functions to access the temporary function
$('#foo').data('bar',options);
// Do stuff here that uses the new options
// Reset the options to include the original success function
$('#foo').data('bar',old_options);
我原以为只显示一次 Temporary Function Called,然而,它似乎用临时回调完全取代了旧的 success 回调。
谁能告诉我为什么以及如何解决这个问题?
更新
我认为 extend 可以解决这个问题,但似乎这个问题可能更深一些。这次我决定发布我的实际代码片段。阅读前请注意以下事项:
SM 只是jQuery 的别名,请忽略它。success 和 error 是提供给函数的参数这是我的代码:
// Get the properties
var properties = $(form).data('autosave');
switch(parameter){
case 'save':
var old_properties = $.extend({},properties);
// Set the new callbacks if they have been supplied
properties.options.success = typeof success!=='undefined' ? success : old_properties.options.success;
properties.options.error = typeof error!=='undefined' ? error : old_properties.options.error;
// Save the properties
$(form).data('autosave',properties);
// Call the save method before setting the interval
SM(form)._as_save();
properties = $.extend({},old_properties);
// Save the old properties
$(form).data('autosave',properties);
// Clear the current interval
clearInterval(properties.interval);
// Call the save method periodically
properties.interval = setInterval(function(){
SM(form)._as_save();
},properties.options.interval);
break;
}
// Save the properties
$(form).data('autosave',properties);
最佳答案
当您运行这段代码时:
var old_options = options;
您没有为以后可以恢复的整个 options 对象制作副本。您只是将引用保存到相同的对象。换句话说,old_options 与 options 是完全相同的对象,因此当您将新值分配给 options.success 时,您正在更改它在 options 和 old_options 中——因为它们是同一个对象。
要解决此问题,您可以使用对象克隆功能来制作对象的副本,之后您可以将其恢复。由于您使用的是 jQuery,因此可以将上面的行更改为:
var old_options = $.extend( true, {}, options );
现在,当您更改 options.success 时,您只是在 options 对象中更改它。 old_options 不受影响,因此您稍后的调用将成功恢复它:
$('#foo').data('bar',old_options);
有趣的是,即使 options.success 是一个异步回调(从名字听起来很像),这仍然可以正常工作。这是因为无论代码稍后调用该 .success() 方法,它们仍应保留对修改后的 options 对象的引用——即使您已经恢复了旧的同时返回到元素的数据中。至少有人可以这样希望;如果其他代码返回 $().data() 以找到 .success 回调,那么您就有麻烦了。
上面的 $.extend() 调用执行了 options 对象的“深度”(递归)复制。也就是说,如果 options 中的其中一个属性本身就是一个对象,它还会克隆该对象,而不是仅仅复制对它的引用。
如果您省略 true 参数,$.extend() 将执行浅拷贝:
var old_options = $.extend( {}, options );
这仍然会创建一个新对象并复制现有对象的所有属性,但如果其中一个属性本身就是一个对象,它不会克隆该对象,它只会复制一个引用。如果它适用于您正在使用的对象的结构,则效率更高,否则您可以使用深拷贝。
如果需要保存和恢复的属性/方法是主对象的直接子对象,浅拷贝就足够了。在这种情况下,您肯定需要深拷贝:
{
url: 'test',
events: {
success: function( data ) {
// ...
}
}
}
这里我们有一个具有events属性的对象,该属性本身就是一个具有一些属性/方法的对象(在这个例子中是一个events.success() 方法。如果您对此对象进行浅拷贝,则原始对象和副本将共享一个公共(public)events 对象。因此,如果您执行如下操作:
options.events.success = function(...) {...};
您实际上会在 options 和 old_options 中更新它。不好。这就是需要深度复制的地方。
关于javascript - 在对象/变量中存储对函数的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17201074/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调