varfoo="i am foo";varobj={__proto__:[],// 定義方法getLength(){returnthis.length;},// 動態設定變數/方法名稱['prop_'+(()=>42)()]:"Should be 42",//foo:foo 的縮寫foo};obj.push("1");console.log(obj.getLength());//1console.log(obj.prop_42);//"Should be 42"console.log(obj.foo);//"i am foo"
classPolygon{constructor(height,width){//class constructorthis.name='Polygon';this.height=height;this.width=width;}staticdoIt(){//class methodconsole.log('Do it now');}}classSquareextendsPolygon{constructor(length){super(length,length);//call the parent method with superthis.name='Square';}getarea(){//calculated attribute getterreturnthis.height*this.width;}}lets=newSquare(5);console.log(s.area);//25Square.doIt();//"Do it now"
varmyString=`InJavaScript'\n'isaline-feed.`;// Multiline stringsvarmyString2=`InJavaScriptthisisnotlegal.`;varname="Bob",time="today";varmyString3=`Hello${name},howareyou${time}?`console.log(myString);//"In JavaScript ' ' is a line-feed."console.log(myString2);//"In JavaScript this is not legal."console.log(myString3);//"Hello Bob, how are you today?"functiontag(strings,...values){if(!(strings[0]==='a'&&strings[1]==='b')){return'bad';}return'good';}console.log(tag`a${123}b`);// "good"console.log(tag`c${123}d`);// "bad"
// same as ES5.1console.log("𠮷".length==2);//true// new RegExp behaviour, opt-in ‘u’//console.log("𠮷".match(/./u)[0].length == 2); //在ES6模擬器上無法使用// new formconsole.log("\uD842\uDFB7"=="𠮷");//true//console.log("\u{20BB7}" == "𠮷"); //在ES6模擬器上無法使用// new String opsconsole.log("𠮷".codePointAt(0)==0x20BB7);//true//在ES6模擬器上無法使用// for-of iterates code points//for(var c of "𠮷") {// console.log(c);//}
// Setsvars=newSet();s.add("hello").add("goodbye").add("hello");s.size===2;s.has("hello")===true;// Mapsvarm=newMap();m.set("hello",42);m.set(s,34);m.get(s)==34;// Weak Mapsvarwm=newWeakMap();wm.set(s,{extra:42});wm.size===undefined// Weak Setsvarws=newWeakSet();ws.add({data:42});// Because the added object has no other references, it will not be held in the set
// Proxying a normal objectvartarget={};varhandler={get:function(receiver,name){return`Hello,${name}!`;}};varp=newProxy(target,handler);p.world==='Hello, world!';
12345678910
// Proxying a function objectvartarget=function(){return'I am the target';};varhandler={apply:function(receiver,...args){return'I am the proxy';}};varp=newProxy(target,handler);p()==='I am the proxy';
varpromise=newPromise(function(resolve,reject){// do a thing, possibly async, then…if(/* everything turned out fine */){resolve("Stuff worked!");}else{reject(Error("It broke"));}});promise.then(function(result){console.log(result);// "Stuff worked!"},function(err){console.log(err);// Error: "It broke"});
Number.EPSILONNumber.isInteger(Infinity)// falseNumber.isNaN("NaN")// falseMath.acosh(3)// 1.762747174039086Math.hypot(3,4)// 5Math.imul(Math.pow(2,32)-1,Math.pow(2,32)-2)// 2"abcde".contains("cd")// true"abc".repeat(3)// "abcabcabc"Array.from(document.querySelectorAll('*'))// Returns a real ArrayArray.of(1,2,3)// Similar to new Array(...), but without special one-arg behavior[0,0,0].fill(7,1)// [0,7,7][1,2,3].findIndex(x=>x==2)// 1["a","b","c"].entries()// iterator [0, "a"], [1,"b"], [2,"c"]["a","b","c"].keys()// iterator 0, 1, 2["a","b","c"].values()// iterator "a", "b", "c"//Object copyingObject.assign(Point,{origin:newPoint(0,0)})