一、JavaScript的历史与发展JavaScript诞生于1995年,由Netscape公司的Brendan Eich在短短10天内设计完成。最初命名为Mocha,后改为LiveScript,最终为了借助Java的流行度而定名为JavaScript(尽管两者并无直接关系)。重要历史节点:
- 1996年:微软推出JScript,与Netscape竞争
- 1997年:ECMAScript标准第一版发布(ECMA-262)
- 2005年:AJAX技术兴起,JavaScript重要性提升
- 2009年:ES5发布,带来严格模式、JSON支持等
- 2015年:ES6/ES2015发布,JavaScript现代化
- 2016至今:每年发布新标准,持续演进
Javascript与ECMAScript的关系
JavaScript是ECMAScript的实现之一,完整的JavaScript包括:
- ECMAScript核心:语言基础(语法、类型、语句等)
- DOM:文档对象模型(浏览器文档操作)
- BOM:浏览器对象模型(与浏览器窗口交互)
二、ECMAScript标准详解2.1 版本演进
|
版本 |
发布时间 |
主要特性 |
|
ES1 |
1997 |
初始版本 |
|
ES3 |
1999 |
正则表达式、try/catch |
|
ES5 |
2009 |
严格模式、JSON、数组方法 |
|
ES6 |
2015 |
let/const、类、模块、Promise等 |
|
ES2016 |
2016 |
指数运算符、Array.prototype.includes |
|
ES2017 |
2017 |
async/await、共享内存 |
|
ES2020 |
2020 |
可选链、空值合并、BigInt |
2.2 ES6+核心特性1)块级作用域与let/const
// ES5只有函数作用域和全局作用域var x = 10;if (true) { var x = 20; //
同一个变量 console.log(x); // 20}console.log(x); //
20
// ES6引入块级作用域let y = 10;if (true)
{ let y = 20; // 不同的变量 console.log(y); //
20}console.log(y); // 10
const z = 30;// z = 40; // 错误:不能重新赋值
2) 箭头函数
// ES5函数表达式var multiply
= function(a, b) { return a * b;};
// ES6箭头函数const multiply = (a, b) => a *
b;
// this绑定差异function Counter() { this.count = 0;
// ES5方式需要额外绑定this var self
= this; setInterval(function() { self.count++;
}, 1000);
// ES6箭头函数自动绑定词法this setInterval(()
=> { this.count++; }, 1000);}
3)类语法
// ES5原型继承function Person(name)
{ this.name = name;}Person.prototype.sayHello = function()
{ console.log("Hello, " + this.name);};
// ES6类语法class Person { constructor(name)
{ this.name = name; }
sayHello() { console.log(`Hello, ${this.name}`);
}
// 静态方法 static createAnonymous()
{ return new Person("Anonymous");
}}
class Student extends Person { constructor(name,
grade) { super(name); this.grade =
grade; }}
4)模块系统
// math.jsexport function add(a,
b) { return a + b;}
export const PI = 3.14159;
// app.jsimport { add, PI } from './math.js';console.log(add(2, PI)); //
5.14159
// 默认导出// logger.jsexport default function(message)
{ console.log(message);}
// app.jsimport log from './logger.js';log("Hello
Modules!");
5)Promise与异步编程
// 回调地狱示例getData(function(a)
{ getMoreData(a, function(b) { getMoreData(b, function(c)
{ console.log(c); }); });});
// Promise解决方案getData() .then(a => getMoreData(a))
.then(b => getMoreData(b)) .then(c => console.log(c))
.catch(error => console.error(error));
// async/await语法糖async function fetchData()
{ try { const a = await getData();
const b = await getMoreData(a); const c
= await getMoreData(b); console.log(c);
} catch (error) { console.error(error); }}
2.3 最新的ECMAScript特性1)可选链操作符(?.)
const user = { profile:
{ name: "John", address:
{ city: "New York" }
}};
// 传统方式const city = user && user.profile &&
user.profile.address && user.profile.address.city;
// 可选链const city = user?.profile?.address?.city; //
"New York"const zipCode = user?.profile?.address?.zipCode; //
undefined而不是错误
2) 空值合并运算符(??)
// 传统方式const configValue
= (config.setting !== null && config.setting !== undefined)
? config.setting : 'default';
// 空值合并const configValue = config.setting ?? 'default';
// 与||的区别0 || 'default' //
'default' (0是falsy)0 ?? 'default' //
0 (只有null/undefined会触发默认值)
3)动态导入
// 静态导入// import
{ largeModule } from './largeModule.js'; // 初始加载
// 动态导入button.addEventListener('click', async ()
=> { const module = await import('./largeModule.js'); module.largeFunction();});
三、JavaScript运行环境3.1 浏览器环境
浏览器提供完整的JavaScript执行环境,包括:
- JavaScript引擎:解析和执行代码
- DOM API:文档操作
- BOM API:浏览器窗口操作
- 事件循环:处理异步操作
// 浏览器环境示例document.getElementById('myButton').addEventListener('click', () => { fetch('https://api.example.com/data') .then(response => response.json()) .then(data => { localStorage.setItem('userData', JSON.stringify(data)); history.pushState({}, '', '/new-page'); });});
3.2 Node.js环境
Node.js基于Chrome V8引擎,提供了服务器端JavaScript运行环境:
- CommonJS模块系统
- 文件系统访问
- 网络操作
- 原生扩展支持
// Node.js示例const http
= require('http');const fs = require('fs').promises;
const server = http.createServer(async (req, res) => { try {
const data = await fs.readFile('./index.html');
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data); } catch (err) { res.writeHead(500);
res.end(err.message); }});
server.listen(3000, () => { console.log('Server running
on http://localhost:3000');});
3.3 其它运行环境
- Deno:安全的TypeScript运行时
- Bun:高性能JavaScript运行时
- Electron:桌面应用开发
- React Native:移动应用开发
![]() |
Austin Liu 刘恒辉
Project Manager and Software Designer E-Mail:lzhdim@163.com Blog:https://lzhdim.cnblogs.com 欢迎收藏和转载此博客中的博文,但是请注明出处,给笔者一个与大家交流的空间。谢谢大家。 |




浙公网安备 33010602011771号