草庐IT

JavaScript快速入门-06-函数

全部标签

javascript - react : Prevent scroll when modal is open

我有一个自定义模态组件。当它打开时,后台没有任何滚动。我试过下面这段代码:componentDidMount(){document.body.style.overflow='hidden';}componentWillUnmount(){document.body.style.overflow='unset';}一开始似乎可行,但是当我使用模态组件时,在另一个页面中,即使模态关闭也没有滚动。有更好的解决方案吗?我的模态组件:exportclassModalextendsReact.Component{constructor(props){super(props);}componentD

javascript - 如果满足两个条件,则 Javascript 值的总和

这个问题在这里已经有了答案:Groupobjectsbymultiplepropertiesinarraythensumuptheirvalues(16个答案)关闭3年前。如果CategoryId相同,下面的代码会添加金额,并根据CategoryId创建一个新的行项目。self.OriginalLineItems=[{CategoryId:'Cat1',Amount:15,Type:'TypeA'},{CategoryId:'Cat1',Amount:30,Type:'TypeA'},{CategoryId:'Cat1',Amount:20,Type:'TypeB'},{Categor

javascript - 了解异步 React 渲染

我刚开始学习React,我想知道为什么下面的代码不能按预期工作。我以为它会显示Thenumbers:0123但它只显示0。我也对基于类的组件使用了相同的方法,并使用了钩子(Hook),但我仍然得到相同的结果。我对使用异步代码进行react渲染有什么不理解的?importReactfrom"react";importReactDOMfrom"react-dom";functionApp(){letnumbers=[0];fetch("some.url").then(res=>res.json()).then(list=>{for(letnoflist){numbers.push(n);}

javascript - 使用键插值和解构从状态对象中删除属性

当我尝试根据foo变量的值从状态对象中删除一个属性时...const[state,setState]=useState(initialState);...const{[foo],...newState}=state;setState({newState});我遇到了这个SyntaxErrorParsingerror:Unexpectedtoken,//theoneafter[foo] 最佳答案 您不能像那样解构动态键,并且使用丢失的键更新状态将使该键保持不变,因此它不会从状态中删除。您可以将状态变量设置为undefined以将其从状

javascript - 如何在 Metro Bundler 的 metro.config.js 中附加扩展名?

我正在尝试捆绑markdown文件而不产生太多开销(即不将它们手动添加到Xcode和AndroidStudio中的Assets包,不使用第3方依赖项)。我的想法是允许require()通过调整metro.config.js中的metrobundler设置来包含它们:/***MetroconfigurationforReactNative*https://github.com/facebook/react-native**@format*/module.exports={transformer:{getTransformOptions:async()=>({transform:{expe

javascript - 编写一个函数对对象数组进行排序(通过使用另一个对象来指定排序路径和顺序)

我将编写一个函数来对具有某种结构的任何JSON进行排序(只要知道它是一个对象数组,例如产品列表),方法是使用另一个对象作为其参数以确定根据哪个对象执行排序键。//ThejsonthatIgetmightlookslikesomethinglikethis.//Ijustwriteoneitemofthearray,butallofthemarethesame.//Buttheblueprintoftheitemsineachjsonaredifferent.constdataArray=[{id:100,name:'product_1',price:99.95,color:['#fff

Javascript 生成器问题 - 解释这段代码

我正在阅读FlavioScopes的“TheJavaScriptHandbook”。他介绍了生成器的概念。function*calculator(input){vardoubleThat=2*(yield(input/2))varanother=yield(doubleThat)return(input*doubleThat*another)}//Hethenrunsthefollowingcodeconstcalc=calculator(10)console.log(calc.next())输出{value:5,done:false}calc.next(7);输出:{value:14

javascript - 按属性对三个不同的对象数组进行排序

我有三个不同的对象数组,我需要对Date字段进行排序,其中每个组中的字段具有不同的名称。下面是我的数据示例:constdocuments=[{documentId:'ADB0125A',fileName:'test_2018.pdf',date':'2017-12-02T19:08:52+01:00'//Fieldtosortby},{documentId:'123456',fileName:'test2_2018.pdf',date':'2017-12-12T22:08:52+01:00'//Fieldtosortby},{documentId:'121212',fileName:'

javascript - yarn 无法使用 node 12.1.0 构建 node-sass

我正在尝试使用yarninstall安装我的包我安装了node12.1.0,据我所知,node-sass需要至少版本4.12+才能工作Node12我的package.json文件是这样的:{"name":"redacted","private":true,"dependencies":{"@rails/webpacker":"3.5","babel-preset-react":"^6.24.1","bootstrap":"^4.1.2","font-awesome":"^4.7.0","js-md5":"^0.7.3","moment":"^2.22.2","prop-types":"

javascript - 迷你 javascript 电子表格系统中 'with' 的替代方案

我刚刚遇到了这个我以前从未见过的优雅的javascript电子表格代码:http://jsfiddle.net/ondras/hYfN3/它使用名为getter对象的单元格引用作为DATA对象的属性,并使用“with”来限定单元格值的评估范围。//elm.idisthecellreference,DATAisanobjectwhosepropertiesarethesegetterwrappersObject.defineProperty(DATA,elm.id,{get:getter});魔术发生在getter中://MycommentsbutjsfiddlecodefromOndř