ReactNative前置知识总结

373 阅读2分钟

1. var let const关键字

var关键字

var是 JavaScript 中用于声明变量的最古老的关键字。

  • 作用域:全局作用域和函数作用域
  • 存在变量提升

示例1:

    <script>
            var a= 10
                    function f(){
                            console.log(a)
                    }
            f(); 
            console.log(a);
    </script>

输出结果

10
10

示例2:

<script>
	function f() {

		// It can be accessible any
		// where within this function
		var a = 10;
		console.log(a)
	}
	f();

	// A cannot be accessible
	// outside of function
	console.log(a);
</script>

输出结果

10
ReferenceError: a 未定义

示例3:

<script>
	var a = 10

	// User can re-declare
	// variable using var
	var a = 8

	// User can update var variable
	a = 7
</script>

输出结果

7

示例4:

<script>
	console.log(a);
	var a = 10;
<script>

输出结果

undefined

let 关键字

let关键字是var关键字的改进版本

  • 作用域:块作用域(在特定的block之外无法访问它)
  • 不存在变量提升(所以会存在暂时性死区)

示例1:

<script>
	let a = 10;
	function f() {
		let b = 9
		console.log(b);
		console.log(a);
	}
	f();
</script>

输出结果

9
10

示例2:

let a = 10
function f() {
  if (true) {
    var b = 9

    // It prints 9
    console.log(b)
  }

  // It gives error as it
  // defined in if block
  console.log(b)
}
f()

// It prints 10
console.log(a)

输出结果

未捕获的 SyntaxError:标识符“a”已被声明

实例4:

<script>
let a = 10
if (true) {
	let a=9
	console.log(a) // It prints 9
}
console.log(a) // It prints 10
</script>

输出结果

9
10

示例:

<script>
	console.log(a);
	let a = 10;
</script>

输出结果

Uncaught ReferenceError: Cannot access 'a' before initialization

const关键字

具有let关键字的所有属性,除了用户不能更新它

var、let 和 const 之间的区别

varletconst
var变量的作用域是函数作用域。let变量的作用域是块作用域。const变量的作用域是块作用域。
它可以更新并重新声明到范围内。它可以更新,但不能重新声明到范围内。它不能更新或重新声明到范围内。
它可以在没有初始化的情况下声明。它可以在没有初始化的情况下声明。它不能在没有初始化的情况下声明。
它可以在没有初始化的情况下访问,因为它的默认值是“未定义”。没有初始化就不能访问它,因为它返回一个错误。没有初始化就不能访问它,因为没有初始化就不能声明它。

2. 箭头函数

3. ES6 class

4. promise

5. generate

6. RN中的组件

这块对照官方去看 (View, Text, StyleSheet)

滚动:ScrollView FlatList

RN中触摸事件: 通常用组件 TouchableOpacity TouchableNativeFeedback TouchableHighlight

 onPress
 onBlur
 onFocus
 onLayout
 onLongPress
 onPress
 onPressIn
 onPressOut
 

api请求 Fetch API