在ES5中创建对象的写法:
let name = "江流儿"
let showName = function(){
console.log("name:",this.name);
}
const People = {
name: name,
showName: showName,
func: function(){
console.log("正在西游的路上!");
}
}
console.log(People);
在ES6中创建对象的写法:
let name = "江流儿"
let showName = function(){
console.log("name:",this.name);
}
const people = {
name,//省略了重复的工作
showName,
func(){
console.log("正在西游的路上!");
}
}
console.log(people);
=>
定义函数在ES5中创建函数的写法:
let fn = function(a, b){
...//代码体
}
在ES6中创建函数的写法:
let fn = (a, b) => {
...//代码体
}
this
是静态的,this
始终指向函数声明时所在作用域下的 this
的值,即使使用call、apply、bind
函数修改this
,也不会起作用.//ES5写法
function getName1() {
console.log("ES5:",this);
};
//ES6写法
let getName2 = () => {
console.log("ES6:",this);
};
const people = {
name: "江流儿"
};
getName1.call(people);//people
getName2.call(people);//window
let Person = (name, age) => {
this.name = name;
this.age = age;
}
let stu = new Person("心猿", 5000);
console.log(stu);//err
arguments
变量let fn =() =>{
console.log(arguments);
}
fn(1, 2, 3);//err
1)省略小括号,当形参有且只有一个的时候
let add = n =>{
return n+n;
}
console.log(add(9));//18
2)省略花括号,当代码体只有一条语句的时候,此时的return必须省略,语句的执行结果就是函数的返回值
let pow = n => n * n;
console.log(pow(9));//81
1)点击div 2s 后颜色变成粉色
在ES5中写法:
<style>
div{
width: 200px;
height: 200px;
background-color: #58a;
}
style>
<body>
<div id="box">div>
<script>
let div = document.getElementById("box");
box.addEventListener("click", function(){
// ES5中必须先保存this的值
let _this = this;
// 定时器
setTimeout(function(){
// 修改背景颜色 this
_this.style.background = "pink"
}, 2000);
})
script>
在ES6中使用箭头函数写法:
<script>
let div = document.getElementById("box");
box.addEventListener("click", function(){
// 保存this的值
// let _this = this;
// 定时器
setTimeout(()=>{
// 修改背景颜色 this
// _this.style.background = "pink"
this.style.background = "pink";
}, 2000);
})
script>
2)从数组中返回偶数的元素
const arr = [1, 6, 9, 10, 14, 200];
const result = arr.filter(item => item % 2 === 0);
console.log(result);//[ 6, 10, 14, 200 ]
this
无关的回调。定时器,数组的方法回调this
有关的回调。例如事件回调,对象的方法function func(a, b, c = 10) {
console.log("a:" + a + " b:" + b + " c:" + c);
}
func(10,12);//a:10 b:12 c:10
function conect({ host , username, password, port }) {
console.log(host);
console.log(username);
console.log(password);
console.log(port);
}
conect({
host: "localhost",
username: "root",
password: "root",
port: 3306
})
arguments
...变量
function showData(...args) {
console.log((args));
}
showData(1, 2, 3, 4, 5, 6)//[ 1, 2, 3, 4, 5, 6 ]
...
扩展运算符能将数组转换为逗号分隔的参数序列function showData() {
console.log((arguments));
}
let data = ["江流","心猿","意马"];
showData(data)//不使用扩展运算符
showData(...data)//使用扩展运算符 相当于showData("江流","心猿","意马")
const data1 = ["江流","心猿","意马"];
const data2 = [1, 2, 3, 4];
const newArr = [...data1, ...data2];
console.log(newArr);//["江流","心猿","意马",1, 2, 3, 4];
2. 数组的克隆,如果克隆的数组中有引用数据类型,该方式进行的是浅拷贝
const data1 = ["江流","心猿","意马"];
const newArr = [...data1];
console.log(newArr);//["江流","心猿","意马"];
arguments
可以由rest参数替代,其实在这里转为数组没有意义。function showData() {
let newArguments = [...arguments];
console.log("newArguments:", newArguments);
console.log(Array.isArray(newArguments));//true
}
let data = ["江流", "心猿", "意马"];
showData(...data)
Symbol
,表示独一无二的值。它是JavaScript语言中的第七种数据类型,是一种类似于字符串的数据类型。创建Symbol
语法1:
变量名 = Symbol("标识名")
let name1 = Symbol("张三");
let name2 = Symbol("张三");
console.log("name1:",name1);//name1: Symbol(张三)
console.log("name2:",name2);//name2: Symbol(张三)
console.log(name1 === name2);//false
变量名 = Symbol.for("标识名")
let name1 = Symbol.for("张三");
let name2 = Symbol.for("张三");
console.log("name1:",name1);//name1: Symbol(张三)
console.log("name2:",name2);//name2: Symbol(张三)
console.log(name1 === name2);//true
let name1 = Symbol.for("张三");
let result = name1 + 100; //err
let result = name1 > 100; //err
let result = name1 + "100"; //err
let game = {
up: function () {
console.log("我是原本有的up方法!");
},
down: function () {
console.log("我是原本有的down方法!");
}
}
// 声明一个对象
let methods = {
up: Symbol(),
down: Symbol()
};
game[methods.up] = function(){
console.log("我可以上升!");
}
game[methods.down] = function(){
console.log("我可以快速下降!");
}
console.log(game);
let game = {
up: function () {
console.log("我是原本有的up方法!");
},
down: function () {
console.log("我是原本有的down方法!");
},
[Symbol('up')]: function(){
console.log("我可以上升!");
},
[Symbol("down")]: function(){
console.log("我可以快速下降!");
}
}
console.log(game);
instanceof
运算符,判断是否为该对象的实例时,会调用这个方法class Person{
static[Symbol.hasInstance](param){
console.log(param);
console.log("我被用来检测类型了");
return true;//由你决定是打印true 还是false
}
}
let o ={
name: "江流儿"
};
console.log(o instanceof Person);//返回值由你决定来返回是true 还是false
Symbol.isConcatSpreadable
属性等于是一个布尔值,表示该对象用于Array.prototypt.concat()
时,是否可以展开const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
arr2[Symbol.isConcatSpreadable] = false;
console.log((arr1.concat(arr2)));//[1, 2, 3, Array(3)]