当前位置:网站首页>ES6 -- formal parameter setting initial value, extension operator, iterator, and generating function

ES6 -- formal parameter setting initial value, extension operator, iterator, and generating function

2022-06-25 23:03:00 Climbing procedural ape

Formal parameter setting initial value

characteristic

(1) Be similar to python Set the initial value of the formal parameter of

(2) Parameters with default values , All in the back

            function add(a, b, c = 10) {
                return a + b + c;
            }

            console.log(add(1, 2))  // 13
            let options = [1, 2, 3, 4, 5]
            console.log(add(...options)) // 6

Extension operator

characteristic :

(1) Convert the array to a comma separated sequence of parameters

            const TF_BOYS = [' Jackson Yi ', ' hanah ', ' Juen-Kai Wang ']
            function chunwan() {
                console.log(arguments) // ' Jackson Yi ', ' hanah ', ' Juen-Kai Wang '
            }

            chunwan(...TF_BOYS)

            //  Array merge 
            const KUAI_ZI = [' Wang Taili ', ' Xiao Yang ']
            const FENG_HUANG = [' Zeng Yi ', ' Linghua ']
            console.log(KUAI_ZI.concat(FENG_HUANG))
            console.log([...KUAI_ZI, ...FENG_HUANG]) //  The performance is much higher than concat

			//  Copy of , If there is a reference type , It's a light copy 
			let kuai_zi1 = [...KUAI_ZI]

(2) Object extension operator

Object extension operations can be used for object merging , Regenerate the rest directly into an object, etc , Expanding is similar to a Map

            const CONFIG = {
                host: '127.0.0.1',
                port: '3306',
                username: 'root',
                password: 'root'
            }

            function connect({host, port, ...user}) {
                console.log(host)
                console.log(port)
                console.log(user)
            }

            connect(CONFIG)

            const CONFIG1 = {
                type: 'oracle'
            }
            const CONFIG2 = {
                cmd: 'sqlplus'
            }

            console.log({...CONFIG, ...CONFIG1, ...CONFIG2})

iterator

characteristic

(1)Array、Arguments、Set、Map、String、TypeArray、NodeList Deployed Iterator Interface ( An attribute in an object ) Can use iterators to traverse

(2) You can customize [Symbol.iterator] Method , Define the elements in the traversal object , I feel a little silly

Silicon Valley Web front end ES6 course , cover ES6-ES11_ Bili, Bili _bilibili

            const XIYOU = [' Tang's monk ', ' The Monkey King ', ' Pig eight quit ', ' Monk sha ']
            for (let item of XIYOU) {
                console.log(item)
            }

            for (let item of ' I am the king of ox horn '){
                console.log(item)
            }

Generator function

characteristic :

(1) Asynchronous programming , Pure callback functions

			function * generator() {
			    console.log(111)
				yield ' Never had ears '  // yield Code segmentation , It can be used for segmented debugging ,next Just pass in the arguments , Asynchronous programming parameter passing 
                console.log(222)
				yield ' Never had a tail '
                console.log(333)
				yield ' It's strange. '
                console.log(444)
            }

            let iterator  = generator();
			// iterator.next() // 111
			// iterator.next() // 222
			// iterator.next() // 333
			// iterator.next() // 444
			for (let item of iterator){
			    console.log(item)
			}

Asynchronous programming

JavaScript It's single threaded , Using asynchronous mode can avoid thread switching , Save resources , But the native writing is difficult to read and view

            //  Traditional callback functions 
            setTimeout(() => {
                console.log(' Callback programming ')
	            setTimeout(()=>{
	                console.log(' Wait first 2s Besides, ')
	            },2000)
            }, 2000)
			console.log(' You will see me immediately ')

 Promise Solution --  Study alone

原网站

版权声明
本文为[Climbing procedural ape]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251913429384.html

随机推荐