对于每个参数,当调用者没有传递值时,包含=的部分是个表达式,会默认将值赋予到参数中。所以,animalSentence() 返回 "Lions and tigers and bears! Oh my!,animalSentence("elephants") 返回 "Lions and elephants and bears! Oh my!",animalSentence("elephants", "whales") 返回 "Lions and elephants and whales! Oh my!"。
functionSaferHTML(templateData){vars=templateData[0];for(vari=1;i<arguments.length;i++){vararg=String(arguments[i]);// Escape special characters in the substitution.s+=arg.replace(/&/g,"&").replace(/</g,"<").replace(/>/g,">");// Don't escape special characters in the template.s+=templateData[i];}returns;}
通过这个定义,标签模板SaferHTML <p>${bonk.sender} has sent you a bonk.</p> 会输出为<p>ES6<3er has sent you a bonk.</p>(这结果怎么看都不对啊,作者怎么玩的)。你用户在使用时它都是安全的,即使用户是恶意的用户,如Hacker Steve <script>alert('xss');</script>这样发送一个炸弹。无论代表什么意思。
// Purely hypothetical template language based on// ES6 tagged templates.varlibraryHtml=hashTemplate`<ul>#forbookin${myBooks}<li><i>#{book.title}</i> by #{book.author}</li>#end</ul>`;
从“魔法”这词我想表达什么?对于初学者来说,这个特性是不同于现有JS中的,它一开始就是神秘的。从某种意义上说,它还是语言内部的正常行为(In a sense, it turns the normal behavior of the language inside out)。如果这还不够魔法,我都不知道怎么说了。
function*quips(name){yield"hello "+name+"!";yield"i hope you are enjoying the blog posts";if(name.startsWith("X")){yield"it's cool how your name starts with X, "+name;}yield"see you later!";}
>variter=quips("jorendorff");[objectGenerator]>iter.next(){value:"hello jorendorff!",done:false}>iter.next(){value:"i hope you are enjoying the blog posts",done:false}>iter.next(){value:"see you later!",done:false}>iter.next(){value:undefined,done:true}
classRangeIterator{constructor(start,stop){this.value=start;this.stop=stop;}[Symbol.iterator](){returnthis;}next(){varvalue=this.value;if(value<this.stop){this.value++;return{done:false,value:value};}else{return{done:true,value:undefined};}}}// Return a new iterator that counts up from 'start' to 'stop'.functionrange(start,stop){returnnewRangeIterator(start,stop);}
// Divide the one-dimensional array 'icons'// into arrays of length 'rowLength'.functionsplitIntoRows(icons,rowLength){varrows=[];for(vari=0;i<icons.length;i+=rowLength){rows.push(icons.slice(i,i+rowLength));}returnrows;}
重构复杂的循环。你是否写过庞大而丑陋的函数?你会不会想要把它打散到两个更为简单的部分中?生成器是你重构工具中的新的一把刀子。当你面对复杂的循环时,你可以将生成数据的那部分代码抽离出来,放到一个分离出来的生成器函数中。然后,循环就可以变为for (var data of myNewGenerator(args))。
// Synchronous code to make some noise.functionmakeNoise(){shake();rattle();roll();}// Asynchronous code to make some noise.// Returns a Promise object that becomes resolved// when we're done making noise.functionmakeNoise_async(){returnQ.async(function*(){yieldshake_async();yieldrattle_async();yieldroll_async();});}
// Since jQuery objects are array-like,// give them the same iterator method Arrays havejQuery.prototype[Symbol.iterator]=Array.prototype[Symbol.iterator];
for-of循环在现在所有的发行版本都是支持的,如果你在chrome中打开chrome://flags并启动”Experimental JavaScript”,chrome也会支持。微软Spartan浏览器也支持,但固守的IE版本(in the shiping versions of IE)是不支持的。如果你希望使用新的语法在网页开发中,但是又需要支持IE和Safari,你可以使用如Babel或Google的Traceur,来将ES6代码转化为现在对Web更为友好的ES5。
//letconn=Connection::connect("postgres://postgres:123456@localhost:5432",&SslMode::None).unwrap();letstmt=conn.prepare("SELECT id, name, data FROM person").unwrap();println!("{:?}",stmt.query(&[]).unwrap());letrows=stmt.query(&[]).unwrap();println!("{:?}",rows.get(0));letrow=rows.get(0);println!("{:?}",row.len());println!("id: {:?}",row.columns());letrow_id:i32=row.get(0);letrow_name:String=row.get("name");println!("{:?},,,{:?}",row_id,row_name);//letmutserver=Nickel::new();server.get("**",middleware!(row_name.clone()));server.listen("127.0.0.1:6767");