草庐IT

pattern-matching-with-if-case

全部标签

javascript - 我无法准确理解 JavaScript 的方法 string.match(regexp) 的 g 标志是如何工作的

在《JavaScript:TheGoodParts》一书中解释了方法string.match(regexp)如下:Thematchmethodmatchesastringandaregularexpression.Howitdoesthisdependsonthegflag.Ifthereisnogflag,thentheresultofcallingstring.match(regexp)isthesameascallingregexp.exec(string).However,iftheregexphasthegflag,thenitproducesanarrayofallthem

javascript - knockout js 'with'绑定(bind),数组为空时隐藏

我有一个类别下拉列表,它控制子类别下拉列表。如果所选类别的子类别数组为空,我想隐藏子类别下拉列表。示例代码如下:self.categories=ko.observableArray([{"name":"top1","subcategories":[{"name":"sub1"},{"name":"sub2"}]},{"name":"top2","subcategories":[]}]);self.selected_category=ko.observable();self.selected_sub_category=ko.obserable(); 最佳答案

javascript - if 语句中的多个赋值

为什么我不能这样做:varfooElement,barElements;if(fooElement=document.getElementById('foo')&&barElements=fooElement.getElementsByTagName('bar')&&barElements[0]&&barElements[0].onclick){console.log(barElements[0].onclick);}这也行不通:varfoo,bar;if(foo=true&&bar=true){console.log('yay');} 最佳答案

javascript - preg_match 的 JavaScript 等价物是什么?

这个问题在这里已经有了答案:关闭11年前。PossibleDuplicate:Howcaniusepreg_matchinjQuery?PHPpreg_match功能的jquery等效项是什么?在PHP中它将是:preg_match('/[^a-zA-Z0-9]/',$str);检查字符串是否包含字母和数字以外的任何内容。我想在我的网站上添加一些客户端验证,但我看了又看,找不到与此等效的jQuery。谢谢。

javascript - 你如何在 switch 语句中使用 NaN case?

由于NaN===NaN的计算结果为false,是否可以将NaNcase添加到switch语句?例如,假设我想进行以下切换:switch(x){case1:case2:case4:doSomething();break;caseNaN:doSomethingElse();break;casedefault:doADifferentThing();break;}发送NaN作为x将转到默认情况。我知道有一些方法可以在switch语句中使用NaN(例如,我可以使用if..else语句并使用isNaN),但是有没有更直接的方法? 最佳答案 我

javascript - D3 : use nest function to turn flat data with parent key into a hierarchy

我确信有一种非常简单优雅的方法可以做到这一点,但我不太明白。我有一些看起来像这样的输入数据:[{id:1,name:"Peter"},{id:2,name:"Paul",manager:1},{id:3,name:"Mary",manager:1},{id:4,name:"John",manager:2},{id:5,name:"Jane",manager:2}]如果可能,我想使用d3.js嵌套运算符来获取要在层次结构布局中使用的结构。像这样:[{name:"Peter",children:[{name:"Paul",children:[{name:"John"},{name:"Jan

javascript - JavaScript 中的 "if debug"?

JavaScript或VisualStudio中是否有任何东西可以检测代码是否在Debug模式下使用?在C#中类似于“#ifDEBUG”,但对于JavaScript? 最佳答案 有点晚了,但我需要同样的东西,在找到可行的解决方案之前不能放弃。我有一种“主”javascript文件,其中有一行:Site.DEBUG=false;然后在代码中我可以检查这个常量。现在我需要在构建时解决这个问题,一些自动化会根据项目配置为我设置它。我在这里找到了fnr.exe用于在文件中查找和替换的命令行工具。这是一个非常好的实用程序,无论如何都值得一试。

javascript - AngularJS fn is not a function error using $timeout with a function with parameters 错误

我正在制作一个您可以编辑文本的网页,在您停止输入1秒后,它会自动保存您输入的内容。目前我正在研究$timeout的细节。当我调用没有参数的update方法时,它可以正常工作,但是当我使用参数调用它时,我得到错误:Error:fnisnotafunction$TimeoutProvider/this.$get为什么我在执行以下操作时会收到此错误:timeout=$timeout(update(element,content),1000);但不是当我这样做的时候:timeout=$timeout(update,1000);显然我需要将参数传递给更新方法,因为我需要知道要更新什么。debou

javascript - 避免 React 中的内联函数 : How to bind functions with arguments?

我正在尝试关注no-bindReact使用他们推荐的ES6类模式的规则:classFooextendsReact.Component{constructor(){super();this._onClick=this._onClick.bind(this);}render(){return(Hello!);}_onClick(){//Dowhateveryoulike,referencing"this"asappropriate}}但是,当我需要将参数传递给_onClick时,需要更改什么?我试过类似的方法:import{someFunc}from'some/path';classFoo

javascript - 使用 async 和 await with export const

我无法完成这项工作...它说:await是一个保留字。是的,当然是……而且我想使用它:)怎么了?exportconstloginWithToken=async()=>{returndispatch=>{dispatch({type:SESSION_LOGIN_IN_PROGRESS,payload:true})letstoredData=awaitReadFromLocalDB('user')console.log(storedData)if(!storedData){invalidToken(null,dispatch)}else{storedData=JSON.parse(stor