草庐IT

GL_ELEMENT_ARRAY_BUFFER

全部标签

Javascript 继承 : Parent's array variable retains value

我在这里尝试在JavaScript中使用继承,我发现Parent类中的数组值被Child类继承时出现问题。下面的代码是正常的继承:varParent=function(){this.list=[];};varChild=function(){};Child.prototype=newParent;Child.prototype.constructor=Child;varobj1=newChild;obj1.list.push("hello");console.log(obj1.list);//prints["hello"];当我将新的Child对象(继承包含名为list的数组变量的Pa

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,CSS : Get element by style attribute

我愿意:找到页面中所有元素的样式属性(例如:所有具有color:#333;的元素)为所有这些更改此属性(例如从color:#333更改为color:#444)。您对此有什么建议吗? 最佳答案 我的建议是尽可能避免这样做。相反,使用一个类来分配颜色值,然后您可以使用该类而不是颜色值来查找元素。据我所知,没有选择器(甚至在CSS3中也没有)可用于查询特定样式值,这意味着遍历所有元素(或者它看起来像你可以将其限制为具有style属性的所有元素)并查看element.style.color属性。现在,问题是,即使您在style属性中编写了c

javascript - 将 React.element 转换为 JSX 字符串

我正在尝试构建一个组件,带child和在DOM中渲染子项,并且,在pre中显示子DOM为了记录一种解决方案是将JSX作为单独的prop传递以及。这使得它重复,因为我已经能够通过this.props.children访问它.理想情况下,我只需要以某种方式转换childrenProp作为string这样我就可以在pre中渲染它表明“这段代码产生了这个结果”。这是我目前的情况classDocumentationSectionextendsReact.Component{render(){return{heading||""}{this.props.children}//Changethist

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 - 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 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接受一个可选的第二个