Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common operations of ECMAScript

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

< arr.length ? {value: arr[index++], done: false} : {value: undefined, done: true}; } }}let arr = ['wang',2,'abc',5];let mykiss = Myiterator(arr);console.log(mykiss.next());console.log(mykiss.next());console.log(mykiss.next());console.log(mykiss.next());console.log(mykiss.next()); iterator接口部署到指定的【数据类型】上,可以使用for of遍历数组、字符串、arguments(不能用for,each因为是伪数组),set容器,map容器forEach、for-in与for-of的区别foreach:方法没办法使用 break 语句跳出循环,或者使用return从函数体内返回for in:总是得到对像的key或数组,字符串的下标,而for of和forEach一样,是直接得到值for of:【在ES6中】,增加了一个for of循环,使用起来很简单,for of不能对象用 Generator函数Promise依赖于回调函数,所以推出generator概念: 1.ES6提供的解决异步编程的方案之一 2.Generator函数是一个【状态机】,内部封装了不同状态的数据 3.用来生成【遍历器对象】iterator 4.可暂停函数(惰性求值),yield可暂停,next方法可启动。每次返回的是yield后的表达式结果 特点: 1.function与函数名之间有一个【星号】 2.内部用yield表达式来定义不同的状态 例如: function* generatorExample(){ console.log('开始执行'); let result = yield 'hello'; //状态值为hello yield 'generator';//状态值为geneartor let result = yield 'generator'; console.log(result);//此时的结果为underfined,如果在next方法中参入参数 //返回值就是传参内容aaaaa return '返回的结果'; } let MG = generatorExample();//返回的是指针对象 MG.next(); //每调用一次,返回一个yield后面的值 MG.next(); //每调用一次,返回一个yield后面的值 MG.next('aaaaa');//可以传入 3.geneartor函数返回的是【指针对象】,而不是执行函数内部逻辑 4.调用next方法函数内部逻辑开始执行,遇到yield表达式停止, 返回 -- {value:yield表达式后的结果/underfined,done:false/true} 5.再次调用next方法会从上一次停止时的yield处开始,直到最后 6.yield语句返回结果通常为underfined,当调用next方法时传参内容会作为启动时yield语句的返回 值 对象Symbol.iterator属性,指向遍历器对象let obj = {username:'kobe',age:33};//for of无法遍历对象,可以使用generator进行遍历obj[Symbol.iterator] = function* Test(){ yield 1; yield 2; yield 3;}for(let i of obj){ console.log(i);//打印出来的就是yield后面跟的内容}案例:发起ajax请求 function* sendXML(){ let url = yield getNews('http://localhost:3000/news/ID=2'); yield getNews(url); } function getNews(url){ $.ajax(url,function(data){ let commentURL = data.commentUrl; let url = 'http://localhost:3000' + commentURL; //当获取新闻内容成功后,发送请求获取对应的评论内容 //调用next传参会作为上次暂停时yield的返回值 sx.next(url); }) } let sx = sendXML(); //发送请求获取新闻内容 sx.next(); asnc函数详解及应用ES7的东西概念:真正意义上的去解决异步回调的问题,同步流程表达异步操作本质:Generator的语法糖语法: async function foo(){ await 异步操作; await 异步操作; } 特点: 不需要像Generator去调用next方法,调用await等待,当前的异步操作完成就往下执行 返回的总是Promise对象,可以用then方法进行下一步操作 async取代Generator函数的星号*,await取代Generator的yield 语法上更为明确,使用简单,没有副作用。 案例:发送ajax请求 async function getNews(url){ return new Promise((resolve,reject) =>

{$.ajax ({method:'GET', url, success:data = > resolve (data), error:data = > reject ()})} async function sendXML () {let result = await getNews ('http://localhost:3000/news?id=7');) Result = await getNews ('http:localhost:3000' + result.commentUrl);} sendXML (); detailed use of class class 1. Define class / implement class inheritance through class 2. The constructor 3. 0 is defined by constructor in the class. Create an instance of the class through new. 4. Through extends to achieve class inheritance 5. 5. Call the constructor of the parent class 6. 0 through super. Override the general method inherited from the parent class / / normally create the object function Person (name,age) {this.name=name; this.age=age;} let p = new Person ('kond',22); / / define the class class Person {constructor (name,age) {/ / define the constructor this.name=name; this.age=age using class } getName () {/ / define general methods console.log (this.name);}} let p = new Person ('king',33); p.getName (); / / use class implementation to inherit class StartPerson extends Person {constructor (name,age,salary) {super (); this.salary = salary }} string, array extension 1.includes (str): determine whether to contain the specified string 2.startsWith (str): determine whether to start with the specified string 3.endsWith (str): determine whether to end with the specified string 4.repeat (count): repeat the specified number of times the extension 1. Binary and octal numerical representation: 0b for binary and 0o console.log (0b1010) for octal / / for decimal 10 2.Number.isFinite (I): determine whether it is a finite number 3.Number.isNaN (I): determine whether it is NAN 4.Number.isInteger (I): determine whether it is an integer 5.Number.parseInt (str): convert a string to the corresponding number Number.parseInt ('123abc') / / 123 6.Math.trunc (I): directly remove decimal array extensions: 1.Array.from (v): convert pseudo-array objects or traversable objects to true array 2.Array.of (v1) V2recoveryv3): converts a series of values into an array let arr = Array.of (1 mine2 minefield abcprecinct true) 3.find (function (value,index,arr) {return true}): find the first element that satisfies the condition true let arr2 = [2, item,index, 3, 5, 5, 6, 7, 8]; let result = arr2.find (function (item,index) {return item > 4) }) 4.findIndex (function (value,index) Arr) {return true}): find the subscript object method extension of the first element that satisfies the condition to return true: 1.1.Object.is (v1) V2): judge whether the two data are completely equal. 0 = =-0 / / true NaN = = NaN / / false Object.is (0maireli 0) / / false the underlying comparison is the string value Object.is (NaN,NaN) / / true 2.Object.assign (target,source1). Source2): copy the properties of the source object to the target object let obj = {} let obj2 = {username:'zhang',age:22} Object.assign (obj,obj2) 3. Directly manipulate the _ _ proto__ attribute let obj2 = {} obj2.__proto__ = obj1; deeply clone the basic data type copy: new data will be generated after the copy, and the modified copied data will not affect the original data object / array copy: the copy will not generate new data, but will copy the reference. Modifying the copied data will affect the method of copying the data: 1. Directly assign a value to a variable / / shallow copy 2.Object.assign () / / shallow copy 3.Array.prototype.concat () / / shallow copy 4.Array.prototype.slice () / / shallow copy 5.JSON .parse (JSON.stringify) / / Deep copy! Shallow copy: a reference to a copy. Modifying the data after the copy will affect the deep copy of the original data: generate new data when you copy The modified copied data will not affect the original data set, Map container 1.Set container: disordered and unrepeatable collection of multiple value set () set (array) add (value) delete (value) has (value) clear () size () 2.Map container: disordered key non-repeatable collection of multiple key-value Map () Map (array) set (key Value) / / add get (key) delete (key) has (key) / / to determine whether there is clear () size let set = new Set ([1, 2, 3, 3, 4, 5, 6]) Set.add (7); let map = new Map ([['aaa','username'], [36 aaa','username']]) map.set (78); map.delete (78); for of cycle: 1. Traversal array 2. Traverse set 3. Traverse map 4. Traversal string 5. Traversing the pseudo array so far, I believe you have a deeper understanding of "what are the common operations of ECMAScript?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report