let
、const
和 var
的区别var
:
let
:
const
:
if (true) {
var variable = "var variable";
let letVariable = "let variable";
const constVariable = "const variable";
console.log(variable); // "var variable"
console.log(letVariable); // "let variable"
console.log(constVariable); // "const variable"
}
console.log(variable); // "var variable"(变量提升)
console.log(letVariable); // ReferenceError: letVariable is not defined
console.log(constVariable); // ReferenceError: constVariable is not defined
function functionName() {}
const functionName = () => {}
return
,如果函数体只有一条语句。this
指向:
this
指向取决于调用方式。this
指向在定义时就已经确定,继承自外层作用域的 this
。arguments
对象:
arguments
对象。arguments
对象,但可以访问外层函数的 arguments
。new
关键字:
new
关键字调用。new
关键字调用。const obj = {
id: "OBJ",
a: function () {
console.log(this.id); // "OBJ"
},
b: () => {
console.log(this.id); // "undefined"(在严格模式下)
},
};
obj.a(); // "OBJ"
obj.b(); // "undefined"(在严格模式下)
不能。箭头函数没有自己的 this
,没有 arguments
,没有 prototype
属性,且无法通过 new
关键字调用。
let fun = () => {
console.log("我是箭头函数");
};
new fun(); // TypeError: fun is not a constructor
Symbol
基础数据类型有什么作用?Symbol
是一个唯一的、不可变的数据类型,主要用于定义对象的唯一属性名,避免属性名冲突。
let mySymbol = Symbol('mySymbol');
let obj = {
[mySymbol]: 'value'
};
console.log(obj[mySymbol]); // "value"
require()
导入,module.exports
或 exports
导出。import
导入,export
导出。// CommonJS
// module.js
const cjsFun = () => {
console.log("CommonJS");
};
module.exports = { cjsFun };
// app.js
const { cjsFun } = require("./module.js");
cjsFun(); // "CommonJS"
// ES Module
// module.js
export const esFun = () => {
console.log("ES Module");
};
// app.js
import { esFun } from "./module.js";
esFun(); // "ES Module"
this
指向哪里?箭头函数的 this
指向在定义时就已经确定,继承自外层作用域的 this
。
function Outer() {
this.name = 'Outer';
const InnerArrow = () => {
console.log(this.name); // "Outer"
};
this.innerArrow = InnerArrow;
}
const obj = new Outer();
obj.innerArrow(); // "Outer"
扩展运算符 (...
) 用于将数组或对象展开成一系列参数,或合并多个参数为一个数组。
// 数组展开
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combinedArray = [...arr1, ...arr2]; // [1, 2, 3, 4, 5, 6]
// 对象展开
const obj1 = { a: 1 };
const obj2 = { b: 2 };
const mergedObj = { ...obj1, ...obj2 }; // { a: 1, b: 2 }
// 字符串展开
const str = 'Hello';
const letters = [...str]; // ['H', 'e', 'l', 'l', 'o']
// 函数调用中的参数
function myFunction(x, y, z) {
console.log(x, y, z);
}
const args = [1, 2, 3];
myFunction(...args); // 1 2 3
Proxy
是一个强大的工具,允许我们创建一个对象的代理,并拦截对该对象的各种操作,从而实现自定义行为。
const handler = {
get: (target, prop) => {
if (prop in target) {
return target[prop];
} else {
throw new Error(`Property ${prop} does not exist.`);
}
}
};
const proxy = new Proxy({}, handler);
proxy.a = 1;
console.log(proxy.a); // 1
数组解构:
const [a, b, c] = [1, 2, 3];
console.log(a); // 1
console.log(b); // 2
console.log(c); // 3
对象解构:
const user = { name: 'Alice', age: 25 };
const { name, age } = user;
console.log(name); // "Alice"
console.log(age); // 25
使用对象解构赋值来提取深度嵌套的对象属性。
const deepObject = {
level1: {
level2: {
level3: {
targetProp: 'desired value'
}
}
}
};
const { level1: { level2: { level3: { targetProp } } } } = deepObject;
console.log(targetProp); // "desired value"
Rest 参数用于表示不确定数量的参数,将传入的多个参数包装成一个数组。
function sum(...numbers) {
return numbers.reduce((acc, curr) => acc + curr, 0);
}
console.log(sum(1, 2, 3, 4)); // 10
模板字符串使用反引号 () 包裹字符串,支持插值表达式
${}`。
const name = "Alice";
const greeting = `Hello, ${name}!`;
console.log(greeting); // "Hello, Alice!"
includes()
:判断是否包含另一个字符串。trim()
:去除字符串两端的空格。repeat()
:将原字符串重复 n 次。replaceAll()
:将所有匹配的字符串替换为新字符串。split()
:根据指定的分隔符,将字符串分割成数组。slice()
:截取指定位置的字符串。substring()
:截取指定位置的字符串。startsWith()
:判断是否以某个字符串开头。endsWith()
:判断是否以某个字符串结尾。let str = "Hello, world!";
console.log(str.includes("world")); // true
console.log(str.trim()); // "Hello, world!"
console.log(str.repeat(2)); // "Hello, world!Hello, world!"
console.log(str.replaceAll("Hello", "Hi")); // "Hi, world!"
console.log(str.split(',')); // ["Hello", " world!"]
console.log(str.slice(7, 12)); // "world"
console.log(str.substring(7, 12)); // "world"
console.log(str.startsWith("Hello")); // true
console.log(str.endsWith("world!")); // true
ES6 引入了许多新特性,包括 let
和 const
、箭头函数、模板字符串、解构赋值、扩展运算符、Proxy
等,这些特性极大地提升了 JavaScript 的开发效率和代码可读性。