考虑这个功能:
function Foo(){
var a = "3";
};
Run Code Online (Sandbox Code Playgroud)
根据__proto__ VS. JavaScript中的原型,
Foo.__proto__ = Function.prototype
Function.prototype.__proto__ = Object.prototype
Run Code Online (Sandbox Code Playgroud)
我理解了这一部分,但如果我在Google Chrome控制台中执行此操作:
Object.__proto__
output: ƒ () { /* native code */ }
Function.__proto__
output: ƒ () { /* native code */ }
Run Code Online (Sandbox Code Playgroud)
Q1:他们为什么指向功能?它们实际上是什么Function以及Object它们如何彼此不同,因为Object实际上是一个函数?:
typeof Object
"function"
Run Code Online (Sandbox Code Playgroud)
Q2:如果一切都是JavaScript中的对象,那为什么是Object函数?另外,如何在JavaScript中实际实现一个函数?在函数内声明的变量会发生什么?函数是否由JavaScript编译器转换为对象?
对不起,如果我错过了一些明显的事 我真的很困惑函数和对象在JavaScript中的实现方式.
我在leetcode中做了这个问题。
请求:
计算两个整数a和b的总和,但不允许使用+和-运算符。
我不明白它提供的解决方案
有人可以解释一下此getSum功能的工作原理吗?
这是JS中的答案:
var getSum=function(a,b) {
const Sum = a^b; //I can't understand how those two line's code can
const carry = (a & b) << 1; //get the sum
if(!carry) {
return Sum
}
return getSum(Sum,carry);
};
console.log(getSum(5,1));Run Code Online (Sandbox Code Playgroud)
React引入了getDerivedStateFromProps(props, state)在每个渲染方法之前调用的新静态方法,但为什么呢?在改变道具之后调用它对我来说是有意义的,但在setState它没有之后,也许我错过了一些东西.
我datePicker根据公司要求创建了一个组件,组件日期由prop控制.我在组件中有以下状态.
selectedDate: number;
selectedMonth: number;
selectedYear: number;
currentMonth: number;
currentYear: number;
view: string;
Run Code Online (Sandbox Code Playgroud)
selected表示从日期道具派生的选定日期,currentMonth并currentYear表示当前日历视图中的月份和年份.
如果date从道具的变化selected*,currentMonth并currentYear应相应改变.为此,我正在使用,getDerivedStateFromProps但是让用户点击月份名称将日历视图切换到月份(而不是显示月份的日期名称),该函数currentMonth使用setState 更新为此,但是prop的日期与之前(包含前一个月)应该,但是getDerivedStateFromProps被调用,currentMonth再次与之前相同,而不是更改.
我正在创建一个额外的变量state来跟踪是否getDerivedStateFromProps被调用,setState但我不认为这是正确的方法.
无论是我做错了什么或丢失了什么或者getDerivedStateFromProps不应该被打电话setState.可能我做错了什么.
在我的项目中,我使用 TS 3.7.2,它应该支持可选链接。但是当我尝试这样使用它时:const state = urlParams.state?.toString()我收到错误:
当前未启用对实验性语法“optionalChaining”的支持
将 @babel/plugin-proposal-optional-chaining ( https://git.io/vb4Sk ) 添加到 Babel 配置的“插件”部分以启用转换。
我查看了发行说明,没有看到有关为该功能添加 tsconfig 选项的任何要求。
我想知道我已经在使用 TS 时是否需要 babel 插件和配置,我应该如何修复错误?
我在Web和SO上搜索了很多内容,在reactiflux聊天中询问,但是没有找到一种干净的,没有漏洞的方法来根据路径/路径呈现某些组件。
假设我有<Header />应该显示在某些页面上的内容,应该在其他页面上隐藏的内容。
确保可以在Header组件中使用此字符串
if (props.location.pathname.indexOf('/pageWithoutAHeader') > -1) return null
Run Code Online (Sandbox Code Playgroud)
如果/pageWithoutAHeader是唯一的,那完全没问题。如果我需要5页具有相同的功能,它将变为:
if (props.location.pathname.indexOf('/pageWithoutAHeader1') > -1) return null
if (props.location.pathname.indexOf('/pageWithoutAHeader2') > -1) return null
if (props.location.pathname.indexOf('/pageWithoutAHeader3') > -1) return null
Run Code Online (Sandbox Code Playgroud)
是的,我可以将路由存储在数组中并编写一个循环,这将更可重用代码。但这是处理此用例的最好,最优雅的方法吗?
我认为这甚至可能是错误的,例如,如果我不使用路由呈现页面的标题,/xyz而我却使用带有UUID的路由,例如/projects/:id和id=xyzfoo,/projects/xyzfoo则不会显示标题,但应该显示标题。
reactjs react-router react-router-v4 react-router-dom react-router-v5
我使用BARCHART从Recharts库,但我面对的问题,同时增加酒吧之间的空间。我尝试使用属性barGap={20} barCategoryGap={20},但是它似乎不起作用。
<ResponsiveContainer width="100%" height={230}>
<BarChart layout="vertical" width="100%" height={600} data={data} barGap={100} barCategoryGap={-20}
margin={{top: 20, right: 20, bottom: 20, left: 20}}>
<CartesianGrid stroke='#f5f5f5'/>
<XAxis interval={0} type="number"/>
<YAxis width={180} dx={-10} interval={0} dataKey="name" type="category"/>
<Tooltip cursor={{fill: '#fff'}}/>
<Legend/>
<Bar dataKey='pv' barSize={18} fill='#f19135' radius={[0,20,20,0]} background={{fill: "#FFF"}}/>
</BarChart>
</ResponsiveContainer>
Run Code Online (Sandbox Code Playgroud) 我使用这个片段在我的 Next.js 应用程序中初始化 GTM:
const tagManagerArgs = {
gtmId: "GTM-XXXXXX"
};
TagManager.initialize(tagManagerArgs);
Run Code Online (Sandbox Code Playgroud)
但是当我尝试用它启动应用程序时,我收到错误:
ReferenceError: document is not defined
at Object.dataScript (/Users/username/work/projectname/node_modules/react-gtm-module/dist/TagManager.js:11:18)
Run Code Online (Sandbox Code Playgroud)
怎么解决这个问题呢?
我有一个特定的案例,如果用户路由说主题/渲染,我需要给他展示一个单独的组件,而当用户路由到主题/ 10时,我需要给他展示另一个组件。我现在面临的问题是,即使执行主题/渲染,我也看到两个组件都在屏幕上渲染。另外,当用户路由到topic / math / 10时,我需要将他路由到其他页面。
这是我的App.js(默认App.js)中的路由部分
<div className="App">
<Router>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route exact path="/topics" component={Topics} />
<Route exact path ="/topics/rendering" component={Description}/>
<Route exact path="/topics/:id" component={Topic} />
<Route path="/topics/:description/:id" component={TopicExtended}/>
</Router>
</div>
Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的 MBP 上设置 nvim,除了最后一步(即安装 nvim-treesitter)之外,一切都很顺利,在短暂的安装例程之后,它会抛出此错误:
nvim-treesitter[phpdoc]: Error during `npm install` (required for parser generation of phpdoc with npm dependencies)
npm WARN old lockfile
npm WARN old lockfile The package-lock.json file was created with an old version of npm,
npm WARN old lockfile so supplemental metadata must be fetched from the registry.
npm WARN old lockfile
npm WARN old lockfile This is a one-time fix-up, please be patient...
npm WARN old lockfile
npm ERR! code 1
npm ERR! path /Users/anton/.local/share/nvim/tree-sitter-phpdoc/node_modules/tree-sitter-cli
npm …Run Code Online (Sandbox Code Playgroud) VSCode 更新后立即收到此错误消息Definition for rule 'testing-library/no-debug' was not found.。尝试安装@types/testing-library,但没有帮助。
我有一个带有许多API请求的前端应用程序,但是使用错误响应很痛苦。
有时我需要遍历许多嵌套对象,例如:error.response.data.email有时是error.response.data.address.province[0]。
我无法预测所有错误,因此编写手动“解析器”对我来说似乎是一个肮脏的额外解决方案:
const errorsObject = err.response.data
let errors = ''
Object.keys(errorsObject).map(key => {
if (key === 'address') {
Object.keys(errorsObject.address).map(
key => (errors += `${key} : ${errorsObject.address[key]}\n`)
)
} else {
errors += `${key} : ${errorsObject[key]}\n`
}
return undefined
})
yield toast.error(errors)
Run Code Online (Sandbox Code Playgroud)
而且它仍然不能涵盖所有内容。
是否有任何前端解析器?如果没有,我们的后端是Python(Django),也许后端有一个软件包?理想情况下,我希望看到一系列平面的对象{title, message}。
javascript ×5
reactjs ×5
api ×1
apple-m1 ×1
html ×1
neovim ×1
next.js ×1
prototype ×1
react-redux ×1
react-router ×1
recharts ×1
treesitter ×1
tsconfig ×1
typescript ×1