ECMAScript:标准 javascript:标准语言实现, actionslipt :flash开发用,也是一种实现
-ECMAScript 6.0 : ECMAScript脚本语法 DOM BOM ---2015年
-解决实际应用中的隐患:var等问题
-es6核心常用知识
[1]变量定义变化:let,const,
-var 变量重复定义 ---let解决,不允许重复定义变量
-变量提升机制 ---let 初始化之前无法访问
-块级作用域的问题,代码块 + for(){} 等有效
const:定义常量,不支持修改;必须初始化。
-支持修改对象的属性值
闭包:本质是函数内部的 函数访问内部函数的外部变量;甚至在函数作用域周期结束仍然刻意访问——原因是闭包的存在。【
比如:访问函数返回内部函数调用,而内部调用函数访问外部变量;此时返回的函数调用在整体函数外部依然可以正常执行。
】
//模板字符串:保持
let name = ‘macro‘;
let age = 33;
// let str = `My name is ${name}`;
let str = ‘My name is ${name},I am ${age}.‘;
//本质是通过正则替换
let str1 = str.replace(/\$\{([^}]*)\}/g, function() {
console.log(arguments);
// return 124;
return eval(arguments[1]);
});
// str = str.replace(/\$\{\w\}/, age);
console.log(str1);
对象特性
//对象克隆,将第二个参数后面的所有对象克隆到第一个参数对象中
let nameObj = { name: ‘macro‘ };
let ageObj = { age: 25 };
let sexObj = { sex: ‘male‘ };
let newObj = {};
Object.assign(newObj, nameObj, sexObj, ageObj);
console.log(newObj);
=======================
//Object.setPrototypeOf()
let nameObj = { name: ‘macro‘ };
let nameObj1 = { name2: ‘macro2‘ };
let obj = {};
Object.setPrototypeOf(obj, nameObj);
console.log(obj.name);
console.log(Object.getPrototypeOf(obj));
=========================
//判断相等 === 无法判断相等时
console.log(Object.is(1, 3));
console.log(NaN === NaN); // false
console.log(Object.is(NaN, NaN)); // true
异步问题
//异步问题
//正常:1 2 3
for (let index = 0; index < 3; index++) {
setTimeout(() => {
console.log(index);
});
}
//错误 3 3 3
for (var index = 0; index < 3; index++) {
setTimeout(() => {
console.log(index);
});
}
//闭包实现//正常:1 2 3
for (var index = 0; index < 3; index++) {
(function(index) {
setTimeout(() => {
console.log(index);
});
})(index);
}
// 面试题
// 2 2
var arr = [];
for (var i = 0; i < 2; i++) {
arr[i] = function() {
console.log(i);
};
}
//注意此处调用的i为全局的i;
// 1 2
arr[0]();
arr[1]();
var arr = [];
for (let i = 0; i < 2; i++) {
arr[i] = function() {
console.log(i);
};
}
arr[0]();
arr[1]();
解构问题
//===对象结构
let { a, b } = { name: ‘macro‘, age: 25 };
console.log(a, b); // undefined undefined
//注意:属性名称必须一致
let { name, age } = { name: ‘macro‘, age: 25 };
console.log(name, age); // macro 25
//更改名称——解构重命名
let { name: userName, age } = { name: ‘macro‘, age: 25 };
console.log(userName, age);
//
let [{ name }, { hobby }] = [{ id: 1, name: ‘macro‘, age: 25 }, { id: 2, name: ‘面壁‘, hobby: ‘swimming‘ }];
console.log(name, hobby);
//省略解构
let [, , c] = [1, 2, 3];
console.log(c);
//默认解构
let { name, age = 20 } = { name: ‘macor‘ };
console.log(name, age);
//Ajax应用
function ajax(options) {
var method = options.method || ‘get‘;
}
//
function ajax({ method = ‘get‘, data }) {
var method = options.method || ‘get‘;
}
let name = ‘macro‘;
let age = 33;
// let str = `My name is ${name}`;
let str = ‘My name is ${name},I am ${age}.‘;
//本质是通过正则替换
let str1 = str.replace(/\$\{([^}]*)\}/g, function() {
console.log(arguments);
// return 124;
return eval(arguments[1]);
});
// str = str.replace(/\$\{\w\}/, age);
console.log(str1);
let maxVal1 = Math.max.apply(this, [1, 23, 4, 5]);
console.log(maxVal1);
let maxVal2 = Math.max.apply(null, [1, 23, 4, 5]);
console.log(maxVal2);
原文:https://www.cnblogs.com/macro-renzhansheng/p/13051312.html