草庐IT

test_variable

全部标签

javascript - JavaScript 中全局作用域中存在 undefined variable 的原因是什么?

我发现很多关于undefined作为值的讨论,例如如何检查是否相等等等。但是undefined作为全局变量存在的“工程”原因是什么?对面没有null变量...console.log(undefinedinthis);//logstrueconsole.log(nullinthis);//logsfalse 最佳答案 在JavaScript中,null是一个保留字;undefined不是,但由环境实现作为一个全局变量,其值为undefined。您会注意到您可以更改undefined的值,但不能更改null的值,except严格模式(会

javascript - 运行 "npm test"时没有 console.log 到 STDOUT(开 Jest )

据我所知,在运行脚本时,console.log()应该可以毫无问题地打印到我的控制台的STDOUT。但在我的例子中,我将NPM配置为在从shell发出npmtest时运行Jest,并且测试文件中的任何console.log()都不会在屏幕上打印任何东西。我也尝试使用process.stdout.write()但在运行npmtest时我仍然没有得到自定义输出。我应该如何调试测试脚本中的内容?我不知道这是来自Node、NPM还是Jest的问题。有一个Jestissue看起来和我的很相似,但我仍然无法解决并输出一个简单的字符串;而其余的Jest输出则照常回显。有人遇到过类似的问题吗?编辑1:

javascript - Jest : cannot find module required inside module to be tested (relative path)

我有这个组件:importReactfrom'react';importVideoTagfrom'./VideoTag';importJWPlayerfrom'./JWPlayer';classVideoWrapperextendsReact.Component{//...componentcode}基于某些逻辑在内部呈现另一个组件(VideoTag或JWPlayer)但是当我尝试在一个Jest文件中测试它时我得到错误:找不到模块'./VideoTag'这三个组件在同一个目录中,这就是为什么当我转译它并在浏览器中看到它在运行时它实际上有效但看起来Jest在解析这些相对路径时遇到问题,这

javascript - 中级 JavaScript : assign a function with its parameter to a variable and execute later

我有一个JavaScript函数:functionalertMe($a){alert($a);}我可以这样执行:alertMe("Hello");我想做的是将带有"Hello"参数的alertMe("Hello")赋给一个变量$func,然后稍后可以通过执行$func();之类的操作来执行此操作。 最佳答案 我想添加评论作为答案代码//definethefunctionfunctionalertMe(a){//returnthewrappedfunctionreturnfunction(){alert(a);}}//declaret

javascript - Vue-test-utils:在单个测试中多次使用 $nextTick

我正在为vuelidate编写单元测试在我的组件中进行验证。我发现$touch()方法是异步调用的,所以我需要为expect()使用$nextTick()。当我需要两个nextTick()s用于两个expect()s时,问题就出现了。describe('Validations',()=>{letdataletmyComponentbeforeEach(()=>{data=()=>{propertyABC='notallowedvalue'}myComponent=localVue.component('dummy',{template:'',validations,data})it('

Javascript/Jquery : Mathematically Divide Two Variables

这是我的代码:varframeWidth=400;varimageWidth=$('#inner-image').css('width');varnumberOfFrames=imageWidth/frameWidth;如何使“numberOfFrames”显示为商数?IE。将“frameWidth”和“imageWidth”处理为数字,而不是对象?如果我需要更清楚地解释自己,请告诉我。谢谢! 最佳答案 .css('width')可能返回带有px的值。您可以使用parseInt()只获取数字。varframeWidth=400;va

带有 .test() 的 Javascript 正则表达式

>varp=/abc/gi;>vars="abc";>p.test(s);true>p.test(s);false;当我在Chrome的控制台上运行此代码时,上面有此输出。每次调用'.test()'时,我都会得到不同的值。有人可以向我解释为什么会这样吗?谢谢 最佳答案 该行为是由于“g”修饰符,即匹配三次,第四次不匹配:>varp=/a/gi;>vars="aaa";>p.test(s)true>p.test(s)true>p.test(s)true>p.test(s)false查看类似问题:WhyRegExpwithglobalf

javascript - "$variable"和 "variable"之间有什么区别 - JavaScript - jQuery

我在jQuery的$(document).ready(function()下初始化了2个变量,var1和$var2。什么是这两个变量之间的主要区别(或可能的区别)?var1="var1";$var2="var2";$('#click1').click(function(){alert(var1);});$('#click2').click(function(){alert($var2);});Here是工作fiddle。 最佳答案 没有区别。Javascript允许在标识符中使用$字符,例如变量和函数名称,就像它允许使用字母、数字和

javascript/youtube api - undefined variable YT

我正在创建一个通过YTapi嵌入的YouTube播放器,但我不断收到一个警告,提示变量YT未定义。我可以看到包含了youtubeAPI的脚本,它应该创建变量YT-为什么这不起作用?它适用于我网站的其他地方。这是链接:http://oncreativity.tv/site/single/4/7CtQaTmEuWk和我的代码:$(document).ready(function(){vartag=document.createElement('script');tag.src="http://www.youtube.com/player_api";varfirstScriptTag=doc

javascript - Jquery each() : variable in callback always has last value?

似乎无法弄清楚这里发生了什么。DiscoverDocumentationDownloadDonate$('.navItem').each(function(){$link=$(this).children('a');$link.hover(function(){$link.css('width','224px');},function(){$link.css('width','192px');})});http://jsfiddle.net/Sth3Z/它应该为每个链接都这样做,而不是它只更改最后一个链接,无论将鼠标悬停在哪个链接上。 最佳答案