草庐IT

dispatch_apply

全部标签

javascript - 为什么调用 "apply"而不是直接调用函数?

在查看raphael或g.raphael或其他库的源代码时,我注意到开发人员做了这样的事情:varval=Math.max.apply(Math,data_array);为什么不直接调用函数,比如:varval=Math.max(data_array);谢谢。 最佳答案 默认情况下,Math.max不接受列表。“apply”允许您将列表解压缩为参数,以便max正常工作。 关于javascript-为什么调用"apply"而不是直接调用函数?,我们在StackOverflow上找到一个类似

javascript - 是否有与 javascript 的 apply() 等效的 lua?

在Lua中,有没有等同于Javascript的apply()函数? 最佳答案 尝试使用()和unpackfunctionsayhello(name,message)print("hello"..name.."."..message)endfunc1=sayhelloargs1={"test","howareyou"}func1(unpack(args1)) 关于javascript-是否有与javascript的apply()等效的lua?,我们在StackOverflow上找到一个类似

javascript - 什么时候使用 $scope.$apply() 是安全的?

我想标题已经很清楚我要问什么了。我创建了这个fiddle:http://jsfiddle.net/Sourabh_/HB7LU/13142/在fiddle中,我试图复制一个async场景。这只是一个示例,但在AJAX调用中,如果我不使用$scope.$apply(),则列表不会更新。我想知道每次进行AJAX调用以更新列表时使用$scope.$apply()是否安全,或者是否有其他一些我可以利用的机制?我为复制场景而编写的代码(与fiddle中的相同):HTML{{item.name}}ChangeJSvarmyApp=angular.module('myApp',[]);functio

javascript - 为什么要将 'null' 传递给 'apply' 或 'call' ?

根据thisJavaScriptreference:ThevaluenullisaJavaScriptliteralrepresentingnulloran"empty"value,i.e.noobjectvalueispresent.ItisoneofJavaScript'sprimitivevalues.functiongetMax(arr){returnMath.max.apply(null,arr);}显式传递关键字this不会更清晰,或者至少更具可读性吗?再一次,在这一点上我可能不明白你为什么要使用null。 最佳答案 W

javascript - Array.apply(null, Array(x) ) 和 Array(x) 之间的区别

到底有什么区别:Array(3)//andArray.apply(null,Array(3))第一个返回[undefinedx3]而第二个返回[undefined,undefined,undefined]。第二个可以通过Array.prototype.functions链接,例如.map,但第一个不是。为什么? 最佳答案 有一个区别,一个非常重要的区别。Array构造函数either接受一个数字,给出数组的长度,并创建一个具有“空”索引的数组,或者更准确地说,长度已设置,但数组实际上并不包含任何内容Array(3);//create

javascript - 使用 .bind() 避免 .call() 和 .apply()

我正在寻找一种方法来完成某项任务,那就是从jQuery.when.apply(null,promiseArray).done(...)到when(promiseArray).done(...)您可能知道,.bind()可以用来创建类似默认参数的东西,也可以做一些非常漂亮的事情。例如,而不是总是调用vartoStr=Object.prototype.toString;//...toStr.call([])//[objectArray]我们可以这样做vartoStr=Function.prototype.call.bind(Object.prototype.toString);toStr(

javascript - Function.prototype.apply.bind 用法?

我完全知道usages对于:Function.prototype.bind.apply(f,arguments)Explanation-Usetheoriginal(ifexists)bindmethodoverfwitharguments(whichitsfirstitemwillbeusedascontexttothis)此代码可用于(例如)通过带参数的构造函数创建新函数示例:functionnewCall(Cls){returnnew(Function.prototype.bind.apply(Cls,arguments));}执行:vars=newCall(Something,

javascript - 在 JavaScript 中使用 Function.call.apply 的目的是什么?

我正在浏览JavaScriptGarden当我偶然发现用于创建“快速、未绑定(bind)的包装器”的Function.call.applyhack时。它说:Anothertrickistousebothcallandapplytogethertocreatefast,unboundwrappers.functionFoo(){}Foo.prototype.method=function(a,b,c){console.log(this,a,b,c);};//Createanunboundversionof"method"//Ittakestheparameters:this,arg1,a

javascript - 在 JavaScript 中使用参数调用 apply 方法

我想使用javascriptapply()方法调用一个函数。如果函数没有参数,这就可以正常工作。即functiontest(){console.log(this);}body=document.getElementsByTagName("body")[0];//shortcuttobodyelementtest.apply(body);//returns[objectHTMLBodyElement]但我似乎不能做同样的事情来调用一个有参数的函数:functiontest(msg){console.log(msg);}body=document.getElementsByTagName(

javascript - 如何传播事件?无效状态错误 : Failed to execute 'dispatchEvent' on 'EventTarget' : The event is already being dispatched

我正在尝试将事件从我的window.document传播到此文档中的iframe。当在window.document中捕获事件时,我尝试以下操作:event.preventDefault()(@dispatchTo()).dispatchEvent(event)#@dispatchTo()returnsthereferenceof`document.querySelector('iframe').contentDocument`但是我得到了InvalidStateError:Failedtoexecute'dispatchEventon'EventTarget':Theeventisa