Let
块的作用域
"use strict";
if(true) {
var fruit = "apple";
}
console.log(fruit);
如果想fruit
变量只在块中有效,那么
"use strict";
if(true) {
let fruit = "apple";
}
console.log(fruit);
此时将会报错,Let
限定变量作用域。
Const
使用Const
声明一个恒量。
"use strict";
const fruit="apple";
console.log(fruit);
恒量不能重新分配新的值
"use strict";
const fruit="apple";
console.log(fruit);
const fruit="Orange";
console.log(fruit);
注意const
限制的是分配恒量的这个动作,而不是里面的值
"use strict";
const fruit=[];
fruit.push('apple');
console.log(fruit);
我们继续往里面添加值是可以的,不会报错
"use strict";
const fruit=[];
fruit.push('apple');
console.log(fruit);
fruit.push("Orange");
console.log(fruit);
如果重新分配fruit
那么将会报错,如下
"use strict";
const fruit=[];
fruit.push('apple');
console.log(fruit);
fruit.push("Orange");
console.log(fruit);
fruit=[];
Array Destructuring
解构数组
获取函数返回数组的值,老的方法是
"use strict";
function fruit(){
return ['apple','Orange','Peach'];
}
var tmp=fruit();
console.log(tmp[0],tmp[1],tmp[2]);
现在我们可以使用解构这种语法
"use strict";
function fruit(){
return ['apple','Orange','Peach'];
}
//这里分别代表三个变量
let [APPLE,ORANGE,PEACH]=fruit();
console.log(APPLE,ORANGE,PEACH);
Object Destructuring
解构对象
"use strict";
function getUser(){
return {'username':'rc','password':123};
}
let {username:myusername,password:mypassword}=getUser();
console.log(myusername,mypassword);
Template Strings
模版字符串
"use strict";
let apple = 'APPLE',
orange = 'ORANGE';
//let fruit = '苹果是:' + apple + '橙是' + orange;
//字符模版的形式 ,注意这里的是反引号不是单引号
let fruit = `苹果是 ${apple} 橙是 ${orange}`;
console.log(fruit);
Tagged Templates
带标签的模版字符串
"use strict";
let apple = 'APPLE',
orange = 'ORANGE';
//let fruit = '苹果是:' + apple + '橙是' + orange;
//字符模版的形式 ,注意这里的是反引号不是单引号
let fruit = shop`苹果是 ${apple} \n
橙是 ${orange}`;
function shop(strings,...values){
console.log(strings);
console.log(values);
}
判断字符串里是否包含其他字符串
"use strict";
let apple = 'APPLE',
orange = 'ORANGE';
//let fruit = '苹果是:' + apple + '橙是' + orange;
//字符模版的形式 ,注意这里的是反引号不是单引号
let fruit = `苹果是 ${apple} 橙是 ${orange}`;
console.log(
//以开头
// fruit.startsWith('苹果')
//以结尾
// fruit.endsWith('橙子')
//包含
fruit.includes("苹果")
);
Default Parameter Values
默认参数
'use strict';
function user(user = 'rc', age = 18) {
return `${user}${age}`
}
console.log(
//可以不传入默认参数
user()
);
也可以传入参数
'use strict';
function user(user = 'rc', age = 18) {
return `${user}${age}`
}
console.log(
user('rc',20)
);
Spread
展开操作符
'use strict';
let user=['rc',18],
password=[123,...user];
console.log(user);//["rc", 18]
console.log(...user);//rc 18
console.log(password);//[123, "rc", 18]
Rest
剩余操作符
"use strict";
//输入多个参数,后面的将存在foods数组里面
function breakfast(desert,drink,...foods){
// console.log(desert,drink,foods);
//展开输出
console.log(desert,drink,...foods);
}
breakfast('蛋糕','啤酒',"apple",'orange');
Destructured Parameters
使用解构对象的方法很容易的为函数设置一个对象的参数
"use strict";
function breakfast(desert,drink,{location,restaurant}={}){
console.log(desert,drink,location,restaurant);
}
//breakfast('cake','bee');//默认参数可以不输入
breakfast('cake','bee',{location:'湛江',restaurant:18});//传入对象
name属性
函数的名字
"use strict";
function breakfast() {
}
let lunch = function() {
}
let myDinner =function dinner(){
}
console.log(
breakfast.name,//breakfast
lunch.name,//lunch 使用匿名函数,函数名字就是变量名
myDinner.name//dinner 函数名字的优先级更加高一点
);
对象表达式
"use strict";
let name = 'rc',
password = '123';
//以前的写法
//let user = {
// username: name,
// userpassword: password,
// breakfast: function() {}
//};
//console.log(user); //{username: "rc", userpassword: "123", breakfast: ƒ}
//如果想添加和变量一样的属性,可以这样
let user={
name,
password,
breakfast(){}
};
console.log(user);//{name: "rc", password: "123", breakfast: ƒ}
对象属性名
"use strict";
let user={};//空白对象
user.name='rc';//name屬性
user['user password']='123';//属性有空格分开,第一种方式
let password='user password2';//第二种方式
user[password]='456';
console.log(user);
Object.is()
对比两个值是否相等
"use strict";
console.log(+0==-0);//true
console.log(+0===-0);//true
console.log(NaN==NaN);//false
console.log(Object.is(+0,-0));//fasle
console.log(Object.is(NaN,NaN));//true
Object.assign()
把对象的值复制到另一个对象里
"use strict";
let user={};
Object.assign(
user,
{username:'rc',
password:'123'}
);
console.log(user);//{username: "rc", password: "123"}
Object.setPrototypeOf()
设置对象的 prototype
"use strict";
let breakfast={
getDrink(){
return `茶`;
}
};
let dinner={
getDrink(){
return `啤酒`;
}
};
let sunday=Object.create(breakfast);
console.log(sunday.getDrink());//茶
console.log(Object.getPrototypeOf(sunday)==breakfast);//true
Object.setPrototypeOf(sunday,dinner);
console.log(sunday.getDrink());//啤酒
console.log(Object.getPrototypeOf(sunday)==dinner);//true