3字符串的拓展_第1页
3字符串的拓展_第2页
3字符串的拓展_第3页
3字符串的拓展_第4页
3字符串的拓展_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、JavaScri ptuxxxx u0061 uD842uDFB7 u20BB7 H 7 OxFFFF; is32Bit() / trueis32Bit(a) / false StringJromCode Point() ES5String.fromCharCode0 xFFFF String.fromCharCode(0 x20BB7) II String. froniCharCode0 xFFFF2 ES6 stringfromCodePoint(0 x20BB7) / String.fromCodePoint(0 x78, 0 xlf680, 0 x79)二二二xuD83DuDE80y

2、true stringfromCodePoint ES6 for (let codepoint of foo) console.log(codePoint) / f7/ 97/ o var text 二 String.fromCodePoint(0 x20BB7); for (let i 二 0; i text.length; i,) consolelog(texti); ” 7/ for (let i of text) console.log(i); II for at() ES5charAt0 xFFFF *abc .charAt(0) / a .charAt(0) / *uD842 ch

3、arAt atexFFFF abc .at(0) / a.at(0) / normalizeO u01Dl=u004Fu030C /falseu01Dl-length / lAu004Fu030C . length / 2 ES6normalize() u01Dl normalizeO = u004Fu030C normalizeO true normalizenormalize NFC NFD NFKCnormalize NFKD u004Fu030C*.normalize(NFC).length / 1Au004Fu030C*.nonmalizeC *NFD).length / 2 NFD

4、 包含字符,字符是否出现在开始位置和结束位置: 传统匕JavaScript只有indexOf方法,可以用來确定一个字符串是否包含在另一个 字符串中。ES6 乂提供了三种新方法。 includesO:返回布尔值,表示是否找到了参数字符串。 startsWithO:返回布尔值,表示参数字符串是否在源字符串的头部。 endsWithO:返回布尔值,表示参数字符串是否在源字符串的尾部。 includesO, startsWith() endsWith() indexOf includesO startsWithO en dsWithO var s = Hello world!; s.startsWit

5、h(* Hello) / trues.endsWith(!) / trues.includes(o) /_ true var s = Hello world!; s.startsWith(*world, 6) / trues.endsWith(Hello, 5) / trues.includes(Hello *, 6) / false nendsWith 字符串重复N次的解决:repeatO repeat xrpQ3t(3) / xxxhello.repeat(2) / hellohello*na.repeat(0) II II na.repeat(2.9) / nana repeat na.

6、repeat(Infinity) Rang eError FA repeat (-1) RangeError na var name = Bob, time = today; undefi ned hello,$name,today is $time; hello,Bob,today is today $(#result *).append( There are + basket.count + * + items in your basket, + + basket.onSale + are on sale!); $(ttresult *).append( There are $basket

7、.count items in your basket $basketonSale are on sale! ./ Tn JavaScript n is a line-feed. / Tn JavaScript this is not legal. console.log(string text line 1 string text line 2); / var name = Bob, time = today; Hello $name, how are you $time? var greeting = Yo World!; $()html( second $C #list) html( s

8、econd .trimO); function authorize(user action) if (user.hasPrivilege(action) throw new ErnorC / / authorized to do is not authorized User */ + / + action/ + . do $action.); / + is not User $ var X l;var y = to 2; .$x $y = $x + y 2 = 3 $y * 2 = $x + y * 2、 4 = 5var obj = x: 1, y: 2

9、; $obj.X + obj.y、 / 3 function fn() return Hello World; bar、 foo Hello World bar toString var msg = HellOj $place; Hello $-World- / Hello World const tmpl = addrs = $addrs.map(addr = $addr.first $addr.last 3 JoinC) const data = first: , last: Bond , first: ,Lans, last: , ,; console. log(tnipl(data);

10、 / / Bond/ Lars/ / Hello Jack!” II let str = return + Hello $name! ;let func = new Function( name, str);func(Dack) / / let str = (name) str);func(Dack) / = Hello $name! *;let func = eval.call(null, Hello Jack! var template = % for(var i=0; i echo();for(var i=0; i data.supplies.length; i+) echo(data.

11、suppliesi); echo(*);echo(); var evalExpr = /g;var expr = /g; template = template .replace(evalExpr,八);n echo( $1 ); n echo( ) .replace(expr, n $1 n echo(); template = echo( + template + *) template var script = (function parse(data) var output = function echo(html) output += html; $ template return

12、output;); return script; function compile(template) var evalExpr = /g; var expr = /g; ternplate = template .replace(evalExpr, n echo( $1 ); n echo( ) .replace(expr 八);n $1 n echo( ); template = *echo( + template + ) var script = 、(function parse(data) var output = function echo(html) output += html;

13、 $ template return output; )、; return script; compile var parse = eval(compile(template); div.innerHTML = parse( supplies: broom mop, cleaner ); / broom/ cleaner/ alert123 / alert(123) var a = 5;var b = 10; tagHello $ a + b world $ a * b 、; tagtag function tag(stringArr, valuel, value2) / / function

14、 tag(stringArr values) / tag tagvaluel tag Hello world tag(Hello - world 15, 50) var a = 5;var b = 10; function tag(s, vl, v2) console.log(s0); console.log(sl); console.log(s2); consolelog(vl); consolelog(v2); return OK; tagHello $ a + b world $ a * b、; / Hello ”/ world 7/ ”/ 15/ 50/ “OK” var total

15、= 30;var msg = passthruThe total is $total ($total*l05 with tax); function passthru(literals) var result = var i = 0; while (i literals.length) result += literaisi+; if (i arguments.length) result += argumentsi; return result; msg / The total is 30 (31.5 with tax) passthru function passthru(literals

16、, .values) var output = for (var index = 0; index values.length; index+) output += literalsindex + valuesindex; output += literalsindex return output; var message = SaferHTML$sender has sent you a message.; function SaferHTML(templateData) var s = templateData0; for (var i = 1; i arguments.length; i

17、+) var arg = String(argumentsi); / Escape special characters in the arg.replace(/) substitution. s += .replace(/g, Sgt;); / Dont escape special characters in the templates += templateDatai; return s; SaferHTML J.18n、Welcome to $siteName, you are visitor number $visitorNumber! II / var libraryHtml =

18、hashTemplate #for book in $myBooks #book.title by #book.author #end JSX $this.state.value jsxjsx java java class HelloWorldApp public static void main(String args) System.out.println(!; / Display the string. HelloWorldApp.main(); tagFirst linenSecond line function tag(strings) console Jog(strings.raw0); / First lineWnSecond line tagstringsstringsFirst linenSecond linen Stri ng.rawO ES6还为原生的String对象,提供了一个raw方法。 String.raw方法,往往用來充当模板字符串的处理函数,返回一个斜杠都被转义(即 斜杠前面再加一个斜杠)的字符串,对应于替换变量后的模板字符串。 ES6 string.raw Stringraw Hin$2+3!; / -HiWnS! String.rawHiu000A!; Hiu000A String.raw String.raw

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论