草庐IT

must_be_array_or_string

全部标签

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

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

javascript - 为什么 Float32Array.length 的值总是 3?

我正在阅读theMozillaDeveloperNetworkdocsonFloat32Arrays当我遇到的时候Float32Array.lengthLengthpropertywhosevalueis3....为什么总是3?我还注意到同名的原型(prototype)属性覆盖了它。 最佳答案 Float32Array实际上是一个函数。你可以这样检查console.assert(typeofFloat32Array==='function');那个函数接受三个参数。引用同一文档中的签名,Float32Array(buffer[,by

javascript - 有人可以解释这个 Array.prototype.find() polyfill 吗?

在此MDN页面上[https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find]有这个polyfill:if(!Array.prototype.find){Object.defineProperty(Array.prototype,'find',{enumerable:false,configurable:true,writable:true,value:function(predicate){if(this==null){thrownewTypeError('Ar

javascript - 语义 ui 的 `gulp build` 给出错误 'ENOENT: no such file or directory'

版本:gulp@3.9.1我已经通过npminstall安装了semantic-ui并在交互式设置过程中给出了默认设置。但是当我从/semantic文件夹执行gulpbuild时,我收到以下错误:[20:52:27]Starting'build'...BuildingSemantic[20:52:27]Starting'build-javascript'...BuildingJavascript[20:52:27]Starting'build-css'...BuildingCSS[20:52:27]Starting'build-assets'...Buildingassets[20:5

javascript - 如何使用 array.map 函数不返回任何内容(空数组)

现在,如果检测到列表中的“Everything”,输出将变为[""]。预期输出:[]Copy.names=rule.names.map(function(x){if(x.name==='Everything'){return'';}else{returnx.name;}}); 最佳答案 使用Array.prototype.filter:Copy.names=rule.names.filter(function(x){returnx.name!=='Everything';}).map(function(x){returnx.name

javascript - "Assertion failed: you need to wait for the runtime to be ready"在JavaScript中调用C函数时出错

我正在尝试一个简单的示例来调用使用JavaScript编译为.wasm的C函数。这是counter.c文件:#includeintcounter=100;EMSCRIPTEN_KEEPALIVEintcount(){counter+=1;returncounter;}我使用emcccounter.c-sWASM=1-ocounter.js编译了它。我的main.jsJavaScript文件:constcount=Module.cwrap('count','number');console.log(count());我的index.html文件只加载正文中的两个.js文件,没有别的:我得

javascript - JavaScript 中 String 对象的属性值

据我了解,每个字符串都是Javascript中的一个对象。尽管如此,它仍然“不起作用”,正如我所期望的那样:vara="abc";//herewegetanewstringobjecta.b=123;//Iseemtodeclareaproperty"b"ofthatobjectalert(a.b);//alerts"undefined"但是,如果我尝试以“错误的方式”定义字符串,一切都会按预期进行vara=newString("abc");//a.b=123;alert(a.b);//alerts"123"为什么会这样? 最佳答案

javascript - Django 管理员 : Pre-populating values from POST or GET?

在我的Django1.2.4站点中,我想将用户引导到一个管理页面,该页面根据他们正在使用的当前数据预先填充了一些值。例如:{%forpersoninpeople%}{{person}}Createafoofor{{person}}{%endfor%}然后,当用户点击链接时,name字段会预先填充值{{person}}。DjangoAdmin界面是否支持这样做?Django管理表单使用POST,但我不确定如何将POST数据添加到来自模板的请求。或者,我可以设置GET变量,然后在表单中使用自定义JavaScript来相应地设置值。 最佳答案

javascript - JQuery JavaScript 设计 : Self Executing Function or Object Literal?

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我很好奇在构造封装代码块时是否有任何与JQuery相关的最佳实践。通常,当我构建一个页面时,我喜欢将该页面中使用的函数封装在一个对象中。这允许我在构建应用程序时进行一些封装。没有什么比看到带有一堆这样的JavaScript文件更让我讨厌的了functiondoSomethingOnlyRelevantOnThisPage(){//dosomestuff}这会导致设计困惑,并且没有很好地封装

javascript - array.forEach 比 native 迭代运行得更快?如何?

http://jsperf.com/testing-foreach-vs-for-loop据我了解,测试用例2的运行速度应该比测试用例1慢——我想看看慢了多少。想象一下当我看到它运行得更快时我的惊讶!这是怎么回事?幕后优化?还是.forEach更干净更快?在WindowsServer2008R2/764位上测试Chrome18.0.1025.14232位 最佳答案 for循环缺少许多迭代优化,例如:缓存数组长度向后迭代使用++counter代替counter++这些是我听说过和用过的,相信还有更多。如果没记错的话,向后迭代while