1.let变量
````// var 声明的变量没有局部作用域
// let 声明的变量 有局部作用域
{
var a = 1;
let b = 2;
}
/// console.log(a)
//console.log(b)
// var 可以声明多次
// let 只能声明一次
var m = 1
var m = 2
let n = 3
let n = 4
console.log(m) // 2
console.log(n) // Uncaught SyntaxError: Identifier 'n' has already been declared
2.const变量
const a =2
a = 3
//console.log(a) //const.html:12 Uncaught TypeError: Assignment to constant variable.
//变量初始化必须赋值
const TYPE //const' declarations must be initialized.
3.对象创建的区别
//原始定义对象和方法
const person = {
sayHi:function(){
console.log("hello,你好")
}
}
person.sayHi();
const person1 ={
sayHi(){
console.log("hello from person1")
}
}
person1.sayHi()
//对象的拷贝 ...
// 1、拷贝对象
let person2 = {name: "elite", age: 22}
let someone = { ...person2 }
console.log(someone) //{name: "elite", age: 22}
4.对象扩展赋值
let user = {name:"elite",age:22,sex:1}
let username = user.name
let userage = user.age
let usersex = user.sex
console.log(username,userage,usersex)
let {name,age,sex} = user
console.log(name,age,sex)
5.模板字符串
let name = "elite"
//模板字符串相当于加强版的字符串,用反引号 `,除了作为普通字符串,还可以在字符串中加入变量和表达式。
let info = `my name is ${name}`
console.log(info)
6.箭头函数
// 当箭头函数没有参数或者有多个参数,要用 () 括起来。
// 当箭头函数函数体有多行语句,用 {} 包裹起来,表示代码块,
// 当只有一行语句,并且需要返回结果时,可以省略 {} , 结果会自动返回。
var f3 = (a,b) => {
let result = a+b
return result
}
console.log(f3(6,2)) // 8
// 前面代码相当于:
var f4 = (a,b) => a+b
console.log(f4(5,6))
7.promise的用法
<script>
//promise的用法
function test(resolve, reject) {
var r = Math.random() * 2;
if (r < 1) {
console.log('call resolve()...');
resolve('200 OK');
}else {
console.log('call reject()...');
reject('r in ' + r + '.');
}
}
//新建promise1
/*var p1 = new Promise(test);
var p2 = p1.then(function (result) {
console.log('成功:' + result);
});
var p3 = p2.catch(function (reason) {
console.log('失败:' + reason);
});*/
//串起来执行
new Promise(test).then(function (result) {
console.log('成功:' + result);
}).catch(function (reason) {
console.log('失败:' + reason);
});
</script>