constructor(props) { super(props); //渲染值设定 this.state = { renderer: 'canvas', }; }什么意思
时间: 2024-05-28 19:09:43 浏览: 13
这段代码是 React 组件的构造函数,它会在组件被创建时被调用。在构造函数中,使用 super(props) 调用父类的构造函数,并将 props 作为参数传递。this.state 是组件的状态对象,其中 renderer 是一个键名,它被初始化为字符串'canvas'。这个状态对象可以在组件中被改变,以便动态地更新组件的内容。
相关问题
constructor(props) { super(props); const detailSaveData = sessionStorageUtil.read('detailSaveData'); this.state = { indocno: this.props.location.state.indocno, type: this.props.location.state.type, selectedTab: detailSaveData ? detailSaveData.selectedTab : 0, data: {}, pathname: window.location.pathname, visibleModal: false, time: new Date(), } } 设置this.props.location.state.indocno为undfind时indocno为“”
可以在 constructor 中添加一个判断语句,来避免出现 undefined 的情况,示例如下:
```
constructor(props) {
super(props);
const detailSaveData = sessionStorageUtil.read('detailSaveData');
const indocno = this.props.location.state.indocno || ""; // 添加判断语句
this.state = {
indocno: indocno,
type: this.props.location.state.type,
selectedTab: detailSaveData ? detailSaveData.selectedTab : 0,
data: {},
pathname: window.location.pathname,
visibleModal: false,
time: new Date(),
};
}
```
这样当 `this.props.location.state.indocno` 为 undefined 时,就会将 `indocno` 设置为空字符串。
function Star(name,x,y,r,Color,revolution){ this.name = name; //星球名称 this.x = x; //圆心 x,y this.y = y; this.r = r; //半径 r this.Color = Color; //颜色 this.revolution = revolution;//公转周期 revolution } ///修改了原来的原型对象,给原型对象赋值的是一个对象.则必须手动的利用constructor指回原来的构造函数 Star.prototype = { constructor : Star,//重新指向原来的构造函数。 drawStar : function(){ cxt.save(); cxt.translate(600,600); cxt.beginPath();需要调用Color
属性来设置颜色,如下所示:
cxt.fillStyle = this.Color;
cxt.arc(this.x, this.y, this.r, 0, 2*Math.PI, false);
cxt.fill();
cxt.closePath();
cxt.restore();
},
moveStar : function(){ //计算星球位置
var angle = Math.PI / 180 * (360 / this.revolution);
this.x = this.x * Math.cos(angle) + this.y * Math.sin(angle);
this.y = this.y * Math.cos(angle) - this.x * Math.sin(angle);
}
};
以上是一个 JavaScript 中的 Star 类,其中包含了构造函数和原型对象。构造函数初始化了星球的名称、位置、半径、颜色和公转周期等属性;原型对象包含了画星球和计算星球位置的方法。在画星球的方法中需要调用 Color 属性来设置星球的颜色。在计算星球位置的方法中,使用了三角函数计算星球的位置。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)