草庐IT

an_array

全部标签

javascript - Array.prototype.slice 奇怪的行为

考虑这段代码,每行末尾都有控制台输出:functionwhatever(){console.log(arguments)//{'0':1,'1':2,'2':3,'3':4,'4':5}console.log(Array.prototype.slice.call(arguments))//[1,2,3,4,5]console.log(Array.prototype.slice.call({'0':1,'1':2,'2':3,'3':4,'4':5}))//[]}whatever(1,2,3,4,5)为什么第三个console.log输出一个空数组? 最佳答案

javascript - Array.filter 无法正常工作

我有一个数组,我想从中删除一条记录我已经使用了Array.filter()但它返回的是相同的数组。我的代码:varurl=window.location.pathname,orderId=url.split('/').slice(-2)[0];varCart=JSON.parse(localStorage.getItem('Cart'));newCart=Cart.filter(function(item){if(parseInt(item.orderId)==parseInt(orderId)){return{};}else{returnitem;}});localStorage.s

javascript - Prototype 在这个 Array slice 调用中,为什么?

我正在阅读JS函数的arguments变量的MDN页面:https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/arguments我知道arguments不是数组,所以这行不通:vara=arguments.slice();MDN上的解决方案是这样做:varargs=Array.prototype.slice.call(arguments);为什么使用Array.prototype而不仅仅是Array.slice.call(arguments)?在这里使用原型(prototyp

javascript - 为什么这不是 google.maps.Map 的 map 实例?无效值错误 : setMap: not an instance of Map;

我收到错误Assertionfailed:InvalidValueError:setMap:notaninstanceofMap;而不是谷歌地图网页上StreetViewPanorama的实例。然后我读了thisquestionStackOverflow上的其他地方告诉我我需要一个google.maps.MAP对象的实例。我认为通过调用该对象来初始化我将调用该对象的map。以前,我收到错误iisnotdefined所以我将createMarker函数移动到$.getJSON函数中,它有本地范围。我需要在其他地方调用google.mapsMap吗?我做错了什么?HTML:HelloWor

javascript - 在 IE 中将函数添加到 Array.prototype 会导致它作为元素被插入每个数组

我在项目的开头将以下polyfill添加到Array:if(!Array.prototype.find){Array.prototype.find=function(predicate){if(this===null){thrownewTypeError('Array.prototype.findcalledonnullorundefined');}if(typeofpredicate!=='function'){thrownewTypeError('predicatemustbeafunction');}varlist=Object(this);varlength=list.leng

javascript - 你能用用户定义的对象伪造 Array.isArray() 吗?

我很好奇是否有任何方法可以用用户定义的对象伪造出Array.isArray()。摘自《JavaScript模式》一书:Array.isArray([]);//true//tryingtofoolthecheck//withanarray-likeobjectArray.isArray({length:1,"0":1,slice:function(){}});//false那个对象显然失败了,但是还有其他方法吗?这纯粹是出于好奇,并不是因为我认为您可以在常规客户端代码中搞砸.isArray()(尽管知道如果可以的话显然会很棒!)。 最佳答案

javascript - Photoshop 脚本 : Move an image to position x, y

我有一个事件的ArtLayer被变量NewLayer引用,我想移动到Canvas中的绝对位置x,y。我已经用谷歌搜索了几个小时,但没有任何结果。有人可以举个例子吗?//谢谢。 最佳答案 经过更多API阅读和搜索后,我得出的结论是只能使用增量移动来移动图层。我编写了这个小函数来将图层定位在绝对位置。希望这对下一个有同样问题的读者有所帮助...//******************************************//MOVELAYERTO//Author:MaxKielland////MoveslayerfLayert

javascript - TypeScript/JavaScript 中的 array.indexOf

这个问题在这里已经有了答案:indexOfmethodinanobjectarray?(29个答案)关闭6年前。更新:虽然这个问题被标记为与this重复.但是@ssube的方法很简洁,也更聪明。更新2:@Grungondola的评论中似乎有新的方法可以做到这一点。我正在使用Typescript。这很有效。vararray1=[];array1.push(5);array1.push(6);console.log("a",array2.indexOf(6));但这并不能很好地工作。因为array2.indexOf返回-1,这意味着它没有找到它。vararray2=[];array2.pu

javascript - 'is an instance of' 在 Javascript 中是什么意思?

这个问题的答案:WhatistheinitialvalueofaJavaScriptfunction'sprototypeproperty?有这样一句话:Theinitialvalueofprototypeonanynewly-createdFunctioninstanceisanewinstanceofObject据我所知,Javascript没有类,因此“实例”这个词在我脑海中没有意义。应该如何解释Javascript中的“实例”?抱歉,我没有足够的代表将我的问题放在该答案的评论线程中。 最佳答案 你说得对,JavaScript

JavaScript 1.6 Array.map() 和 Array.filter() 不使用内置函数作为参数

这很好用:["655971","2343","343"].map(function(x){returnparseInt(x)})//[655971,2343,343]但这不是:["655971","2343","343"].map(parseInt)//[655971,NaN,NaN]Array.filter()也是如此我在这里错过了什么? 最佳答案 这是因为map向回调函数传递的参数不仅仅是数组项。你得到:callback(item,index,array)通常你的函数会忽略它不需要的参数。但是parseInt接受一个可选的第二个