标签:属性 bbb html 数组 john his 函数 get 运行
console.log(a);//返回undefined var a = 10; console.log(b);//返回错误,不存在声明提升, let b = 20; if(a = 10){let c = 30;} console.log(c);//返回错误。存在块级作用域。
let [a,b,c] = [1,2,3];console.log(a,b,c);//返回1 2 3 let [a=3,b] = [2];console.log(a,b);//返回2 undefined; let c; let [a=2] = [c];console.log(a);//返回2,undefined不会覆盖默认值
let {a,b} = {a:"aaa",b:"bbb"};console.log(a,b);//返回aaa bbb; let {a:b} = {a:"aaa"};console.log(b);//返回aaa,如果输出a,则出错!!! let {a,b=5} = {a:1};console.log(a,b);//返回1 5
var obj = {"name":"john","age":18}; var name = obj.name;var age = obj.age;console.log(name + "的年龄是" + age);
let obj = {"name":"john","age":20};
let {name,age} = obj;console.log(`${name}的年龄是${age}`);
<ul id="test"></ul>
var arr = [0,1,2,3,4]; let oUl = document.getElementById("test"); let html1 = ""; for(var i in arr){ /* html += "<li>hao" + arr[i] + "</li>"; } oUl.innerHtml = html;//es5的方法; */ /* let oLi = creatElementWithTxt("li","hao" + arr[i]); oUl.appendChild(oLi); */ html1 += `<li><a href="#">${arr[i]}</a></li>`; //es6使用反引号(``)来拼接多个字符串; } oUl.innerHTML = html1;
var foo = function(){return 1;} console.log(foo());//es5
let foo = () => 1;
console.log(foo()); //es6单条语句
let foo = (a) => a; console.log(foo(10)); //带参数的单条语句
let foo = (a) => {let b = 10;return a + b;} console.log(foo(10)); //多条语句
let foo = () => {console.log(this);} foo();//this 指向根元素window
var name = "window"; let obj = {"name":"john","sayHello":function(){console.log(this.name)}};//es5的写法 let obj = {"name":"john","sayHello":() => {console.log(this.name)}};//es6的写法,指向定义时的作用域,而不是执行时的作用域过程相当于 let obj = {}; obj.name = "john"; console.log(this); obj.sayHello = () => {console.log(this.name);} obj.sayHello();//es5返回john,this 指向obj;es6返回name的值window,指向根元素window;
var name ="window"; let obj = {"name":"john","sayHello":function() {setTimeout(() => { 这里用es5写法则指向obj 如果用es6写法则指向window console.log(this.name); }, 1000);}} obj.sayHello();
for (let [key,value] of map.entries()) {console.log(key,value);}
map.forEach((key,item,value) => {console.log(key * 2);});
function* foo(x){yield x+1; yield x+2; return x+3;} var f = foo(1);//返回GeneratorStatus f.next(1);//才能返回值,2,done:false,done表示是否还有yield存在或者相应的返回值存在或者是否结束 console.log(f.next(),f.next(),f.next());//运行三次最后done:true
function*foo(x){var y = 2 * (yield(x + 1)); var z = yield(y / 3); return(x + y + z)} var it = foo(5); console.log(it.next(3)); console.log(it.next(12)); console.log(it.next(13)); next(n);//参数表示上一次的返回值,第一次传入3的参数是没有上一次的返回值的,第一个返回5+1=6; //而第二次传入12是有上一次的返回值的,12则代表第一个yield(x+1)的值,到下一次yield暂停,返回8; //第三次13也是有上一次的返回值,所以代表yield(y/3),z被赋值13,y之前被赋值24,x最早被赋值5,
function* feibo(n){ let a = 0; let b = 1; for(var i = 0; i < n ; i++){ yield a; let temp = a+ b; a = b; b = temp; } console.log(temp); } var f = feibo(5); for(let i of f){ console.log(i);//把每一次遍历的值赋值给i, }
class Person{
constructor(name){
this.name = name;
}//构造函数属性 sayHello(){//方法
console.log(this.name);
}
} var person = new Person("john");
标签:属性 bbb html 数组 john his 函数 get 运行
原文地址:https://www.cnblogs.com/solaris-wwf/p/11630026.html