1,Alert.alert("参数一:标题", "参数二:内容", [ {text: '确认' , onPress: ()=>{ }} , {text:'取消' , onPress: ()=>{ } } ] );
2,在react native中函数调用什么时候加this,什么时候加()?
加括号表示立即执行,但是一些事件(比如点击触摸),是在事件发生的时候才调用,所以不需要添加括号。而箭头函数代表一个函数,里面调用的函数添加括号以后在内部执行了,在外部并没有执行。
调用函数无参情况下:
<Button onPress={ this.getdata() } /> //会立即执行
<Button onPress={ ()=>{ this.getdata() }) /> //不会立即执行
<Button onPress={ this.getdata } /> //不会立即执行,正确
调用函数有参情况下:
<Button onPress={ this.getdata("string") } /> //会立即执行
<Button onPress={ ()=>{ this.getdata("string1") }} /> //不会立即执行
<Button onPress={ this.getdata.bind(this, "string") } /> //不会立即执行
4,说到这个不得不提一提js中函数,变量调用规则:
function a(){
console.log(1);
}
function a(){
console.log(2)
}
a();
//打印结果是2
a();
var a=6;
function a(){
console.log(3);
}
function a(){
console.log(4);
}
//打印结果是4;
function a(){
console.log(1);
}
function a(){
console.log(2)
}
a();
function a(){
console.log(3);
}
function a(){
console.log(4);
}
//打印结果是4
var a=0;
function a(){
console.log(1);
}
function a(){
console.log(2)
}
a();
function a(){
console.log(3);
}
function a(){
console.log(4);
}
//报错:a is not a function
function a(){
console.log(1);
}
function a(){
console.log(2)
}
a();
var a=6;
function a(){
console.log(3);
}
function a(){
console.log(4);
}
//打印结果4
function a(){
console.log(1);
}
function a(){
console.log(2)
}
a();
function a(){
console.log(3);
}
function a(){
console.log(4);
}
var a=5;
//打印结果是4
5,react native类中的函数调用方式:
class testfunction {
constructor(){
super(props);
_this=this;
this.delete_database=this.delete_database.bind(this);
}
function delete_database(){
Alert.alert("你调用了我,我删除选项一了哦");
}
function onpress_alert(){
Alert.alert( "这是标题", "这是内容",
[ {text: "这是选项一", onPress: _this.delete_database },
{text: "这是选项二", onPress: ()=>console.log("你选择了选项二") }
] }
这里作为一个小白,我踩了很多坑:
1,delete_database()函数作为被调用的,不一定要放在onpress_alert()函数之前,可以放在它后面。
2,delete_database()函数作为onPress事件触发时被调用的回调函数,有两种形式书写:上面已经讲过了
3,听说: 在js中,this指向调用自己的那个对象;而在react native中,this指向函数定义的时候的位置,比如:
class wind{
function a(){
this1=this; //this1指向函数a() 定义时的位置,即类 wind,那么在这里可以调用
}
function b(){
https://www.cnblogs.com/fingersense/p/8098117.html