gpt4 book ai didi

javascript - react js : Why I'm unable to create closure?

转载 作者:行者123 更新时间:2023-11-30 08:32:39 24 4
gpt4 key购买 nike

我有 isLeap 作为检查当前年份是否为闰年的 Prop ,还有另一个 Prop currentMonth 这是当前月份的计数。 (例如 1 表示 2 月)

控制台抛出 "Unexpected token" 错误,当我尝试在一个函数或闭包中创建一个函数时,如下所示

  70 |  },
71 | daysInMonth : function(d){
> 72 | var leapCase = function(this.props.isLeap){
| ^
73 | }
74 | },
75 | render : function(){

在渲染函数中,我通过组件的属性调用上面的函数:

<Week key={i} dayCount = {this.daysInMonth(this.props.currentMonth)} />

最佳答案

您尝试使用参数 this 启动函数,该参数用于确定函数的调用方式。如果你想在闭包中访问 this.props,试试这个。

  70 |  },
71 | daysInMonth : function(){
var props = this.props
72 | var leapCase = function(){
| console.log(props.isLeap)
73 | }
74 | },
75 | render : function(){

在渲染函数中,调用它

<Week key={i} dayCount = {this.daysInMonth.bind(this)} />

关于javascript - react js : Why I'm unable to create closure?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35502434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com