草庐IT

if-null-then-null

全部标签

javascript - Object.create(null) 的用例

我知道使用Object.create(null)创建一个没有proto属性的对象(即Object.getPrototypeOf(myObj)===null)但有人可以帮助我了解这方面的一些用例吗?换句话说,为什么要创建一个完全空的对象(即不从Object.prototype继承任何方法)? 最佳答案 在极少数情况下,可能已将某些内容添加到Object.prototypeObject.prototype.bar='bar';最好用Object.create(null)创建一个Object因为它不会继承这个,考虑一下({}).bar;/

javascript - react : Can I check if a state exists before rendering it

我是React的新手,我制作了一个显示用户名user的导航栏{this.state.name}但问题是如果用户未登录,我会收到一个错误,因为this.state.name未定义。有什么方法可以在将它呈现为导航栏的一部分之前检查是否已定义this.state.name还是有更好的方法来消除此错误? 最佳答案 当然,使用三元:render(){return(this.state.name?{this.state.name}:null);}甚至更短render(){return(this.state.name&&{this.state.n

javascript - Javascript if 语句中的求值顺序

为了保护我的代码不访问我使用的未声明的变量if(typeofmyVar!='undefined')这很好用,但我想在其中添加另一个if语句。像这样转换:if(typeofmyVar!='undefined'){if(myVar=="test"){}}为此:if(typeofmyVar!='undefined'&&myVar=="test")考虑到myVar可能未定义,这最后的代码在每种使用情况和每个浏览器中是否安全?是否有可能if()中的各种语句未按照它们编写的顺序求值?如果myVar未定义,我可以假设myVar=="test"将永远执行吗? 最佳答案

javascript - 拖放 : How to get the URL of image being dropped if image is a link (not the url of the link)

我有这个代码:functiondrop(evt){evt.stopPropagation();evt.preventDefault();varimageUrl=evt.dataTransfer.getData('URL');alert(imageUrl);}FIDDLE如果你放下元素它会提醒图像的url。到目前为止一切顺利。我的问题是,如果您删除元素它会提醒href的url的元素。我想提醒的网址内的元素就像您在上面的示例中放下图像一样。这可能吗?我不介意使用Jquery或任何其他库。我只想获取中图像的url元素。重点是将其他网站的图片链接拖到我的网站并获取图片的url。为了更清楚地说明

javascript - 我如何解决这个 "Cannot read property ' appendChild' of null"错误?

我尝试使用下面的代码,它在我网站的幻灯片中添加了按钮:window.onload=functionloadContIcons(){varelem=document.createElement("img");elem.src="http://arno.agnian.com/sites/all/themes/agnian/images/up.png";elem.setAttribute("class","up_icon");varid="views_slideshow_controls_text_next_slideshow-block";if(id!==0){document.getEl

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

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

javascript - 为什么 Jasmine 被称为 "BDD"测试框架,即使不支持 "Given/When/Then"?

在介绍Jasmine,它说:Jasmineisabehavior-drivendevelopmentframeworkfortestingJavaScriptcode.我阅读了BDD的几篇文章,似乎我们应该使用“Given/When/Then”来定义“Scenario”,这就是“cucumber”所做的。但是在Jasmine中,我看不到任何这样的方法。即使Jasmine没有这样的概念,我们还能称其为“BDD”测试框架吗? 最佳答案 Jasmine不会阻止您使用given-when-then,下面的示例显示了在使用Jasmine时可以

javascript - 使用带有异步函数和 .then 的 MobX @action 装饰器

我正在使用MobX2.2.2尝试在异步操作中改变状态。我将MobX的useStrict设置为true。@actionsomeAsyncFunction(args){fetch(`http://localhost:8080/some_url`,{method:'POST',body:{args}}).then(res=>res.json()).then(json=>this.someStateProperty=json).catch(error=>{thrownewError(error)});}我得到:Error:Error:[mobx]Invariantfailed:Itisnota

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 - JavaScript/jQuery 中的 "if mouseover"或 "do while mouseover"

是否有JavaScript或jQuery解决方案可以在鼠标悬停在DOM对象上时重复运行函数(在setTimeout之后)?否则说,是否有JavaScript“鼠标悬停时执行”(或“如果鼠标悬停”)?$('someObject').bind('mouseover',function(){//Dothefollowingwhilemouseover$('someOtherObject').css('margin-left',adjustedLeft+'px');setTimeout(/*doitagain*/,25);}); 最佳答案