日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

C语言调用es6,ES6 箭头函数、普通函数、调用方法

發(fā)布時(shí)間:2023/12/3 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言调用es6,ES6 箭头函数、普通函数、调用方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

importReact,{Component} from'react';import{

Platform,StyleSheet,Text,Image,View,TouchableOpacity,ToastAndroid,} from'react-native';

export default classsrrowFunDemo extendsComponent {

constructor(props) {

super(props);this.state= {

data0: '點(diǎn)擊0',data1: '點(diǎn)擊1',data2: '點(diǎn)擊2',data3: '點(diǎn)擊3',data4: '點(diǎn)擊4',data5: '點(diǎn)擊5',}

};//箭頭函數(shù)(無(wú)參)press0= () => {

this.setState({

data0: "0被點(diǎn)擊了"});ToastAndroid.show('0被點(diǎn)擊了',ToastAndroid.SHORT);};

//一般方法(無(wú)參)press1() {

this.setState({

data1: "1被點(diǎn)擊了"});ToastAndroid.show('1被點(diǎn)擊了',ToastAndroid.SHORT);};

//一般方法(無(wú)參)press2() {

this.setState({

data2: "2被點(diǎn)擊了"});ToastAndroid.show('2被點(diǎn)擊了',ToastAndroid.SHORT);};

//一般方法(有參)press3(x,y) {

this.setState({

data3: x+y

});ToastAndroid.show('3被點(diǎn)擊了: '+this.state.data3,ToastAndroid.SHORT);};

//一般方法(有參)press4(x) {

this.setState({

data4: x

});ToastAndroid.show('4被點(diǎn)擊了',ToastAndroid.SHORT);};

//箭頭函數(shù)(有參)press5= (x) => {

this.setState({

data5: x

});ToastAndroid.show('5被點(diǎn)擊了',ToastAndroid.SHORT);};

render() {

return(

{/*正確的方式應(yīng)該不在render的時(shí)候立即執(zhí)行。因此正確調(diào)用方法如下,同時(shí),箭頭函數(shù)將一個(gè)函數(shù)賦值給press0變量,變量在調(diào)用的時(shí)候自然不需要加()*/}

// onPress={this.press0} //如果該方法本身就是箭頭函數(shù),那么直接調(diào)用this.press0// onPress={()=>this.press0()}//無(wú)論該方法是箭頭函數(shù)還是普通函數(shù),都可以利用該方法來(lái)執(zhí)行()=>this.press0()onPress={this.press0.bind(this)}//無(wú)論該方法是箭頭函數(shù)還是普通函數(shù),都可以利用該方法來(lái)執(zhí)行()=>this.press0()>{this.state.data0}{/*普通函數(shù)的無(wú)參與有參的調(diào)用方式相同。注意的是有參的函數(shù)使用bind方式傳遞參數(shù)時(shí)this必須在最前面。*/}

onPress={() => this.press1()}

>{this.state.data1}

onPress={this.press2.bind(this)}

>{this.state.data2}

onPress={this.press3.bind(this,2222,1111)}

>{this.state.data3}

onPress={()=>this.press4(2222)}

>{this.state.data4}

{/*下面的調(diào)用方法錯(cuò)誤,原因:下面的調(diào)用方式導(dǎo)致onpress事件直接被調(diào)用press5方法修改了state,由于state被修改,頁(yè)面被重新渲染,再次直接調(diào)用press5形成循環(huán)*/}

{/*正確函數(shù)調(diào)用方式1、箭頭方法 onPress={() => this.press1()} (無(wú)參、箭頭函數(shù)或普通函數(shù),都可以用此方式調(diào)用)2、箭頭方法 onPress={() => this.press1(xx)} (有參、箭頭函數(shù)或普通函數(shù),都可以用此方式調(diào)用)3、bind方式 onPress={this.press2.bind(this)} (無(wú)參、箭頭函數(shù)或普通函數(shù),都可以用此方式調(diào)用)4、bind方式 onPress={this.press2.bind(this,x)}(有參、箭頭函數(shù)或普通函數(shù),都可以用此方式調(diào)用)*/}

//錯(cuò)誤//onPress={this.press5(2222)}//正確onPress={this.press5.bind(this,2222)}

//正確//onPress={()=>this.press5(2222)}>{this.state.data5});}

}

conststyles = StyleSheet.create({

text: {

backgroundColor: 'red',width: 200,height: 30,marginBottom: 50,justifyContent:'center',alignItems: 'center',},});

總結(jié)

以上是生活随笔為你收集整理的C语言调用es6,ES6 箭头函数、普通函数、调用方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。