草庐IT

apply_async

全部标签

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 - jQuery 延迟 : use to delay return of function until async call within function complete + get return value

如何正确使用jQuerydeferreds来延迟函数的返回,直到函数内的异步调用完成+获取返回值?这是我当前的代码:functiongetFields(page){vardff=$.Deferred();result={};$.ajax(//theasynccall{url:page,success:function(data){//workoutvaluesforfield1&field2fromdatahereresult={'field1':field1,'field2':field2};},complete:function(){dff.resolve(result);//my

javascript - react-native async 函数返回 promise 但不是我的 json 数据?

我正在学习react-native,但遇到了一个问题。为什么在从异步函数返回时获取数据会返回一个promise,但在异步函数本身中,它会正确返回一个对象数组?在componentDidMount()上,我调用我的异步函数,该函数依次获取apiurl:componentDidMount(){letdata=this.getData();console.log(data);//在console.log(json)中,我得到了正确的json对象列表,我可以使用json[0].name访问它们。但稍后,console.log(data)返回一个包含奇数数据的promise:Promise{_4

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 - async/await 可以在构造函数中使用吗?

正如问题所述。我可以这样做吗:classMyClass{asyncconstructor(){returnnewPromise()}} 最佳答案 为了扩展PatrickRoberts所说的内容,您不能按照您的要求去做,但您可以改为这样做:classMyClass{constructor(){//staticinitialization}asyncinitialize(){awaitWhatEverYouWant();}staticasynccreate(){consto=newMyClass();awaito.initialize(

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 - 为什么 Array.apply(null, [args]) 在处理稀疏数组时表现不一致?

我最近发现了以下snippetofcodeonSO帮助使用默认值快速填充数组:Array.apply(null,newArray(3)).map(function(){return0;});鉴于Array构造函数和apply方法的行为,上面的代码片段也可以这样重写:Array.apply(null,[undefined,undefined,undefined]).map(function(){return0;});在处理您希望使用默认值填充的稀疏数组时,此技术也很有用:varsparseArr=[3,,,4,1,,],denseArr=Array.apply(null,sparseAr

PHP 和 ZIP 创建 : Can standard zip "options" be applied

有人知道在创建zip文件时如何使用PHPZIP函数来应用标准zip“选项”吗?到目前为止,我所有的搜索都没有找到任何结果。特别是我对应用“-ll”和“-l”选项感兴趣,因此我可以提供WIN或NIX版本的压缩文本文件(cgi和php以及文本文件等)来自nix网站。内容将即时压缩,并为每个特定客户编辑添加的一些文件。我知道构建和使用命令行“反勾号”(Perl)或“passthru(.)”(php)方法,但我希望有使用纯PHP的技巧,即ZipArchive()。问候。 最佳答案 您可能最终会退回到shell_exec并只使用native系