回答: 箭头函数不能作为构造函数的原因:
[[Construct]]
内部方法:普通函数在定义时会创建 [[Construct]]
方法,用于 new
操作符初始化实例,而箭头函数没有此方法。prototype
属性:普通函数的 prototype
属性指向原型对象,用于实例继承,箭头函数无此属性。示例:
const Arrow = () => {};
console.log(Arrow.prototype); // undefined
new Arrow(); // TypeError: Arrow is not a constructor
回答:
特性 | Set | Map |
---|---|---|
存储内容 | 唯一值的集合(无重复元素)。 | 键值对的集合(键可以是任意类型)。 |
API | add(), has(), delete()。 | set(), get(), has()。 |
遍历顺序 | 按插入顺序迭代。 | 按插入顺序迭代。 |
特性 | Map | Object |
---|---|---|
键类型 | 键可以是任意类型(对象、函数等)。 | 键只能是字符串或 Symbol。 |
键顺序 | 保持插入顺序。 | ES6 后对象键按插入顺序,但有例外(如数字键升序排列)。 |
性能 | 频繁增删键值对时性能更优。 | 适合静态键值对。 |
序列化 | 无法直接使用 JSON.stringify。 | 可直接序列化为 JSON。 |
示例:Map 的键可以是对象
const map = new Map();
const keyObj = {};
map.set(keyObj, 'value');
console.log(map.get(keyObj)); // 'value'
回答:
处理 async/await
错误的两种方式:
try/catch
块:
async function fetchData() {
try {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
} catch (error) {
console.error('请求失败:', error);
}
}
Promise 的 .catch()
:
async function fetchData() {
const response = await fetch('https://api.example.com/data').catch(error => {
console.error('请求失败:', error);
});
if (!response) return;
const data = await response.json();
}
错误处理的最佳实践:
try/catch
。
回答:
width
, height
, margin
, position
等)。color
, background
, visibility
等属性。优化建议:
减少重排:
transform
替代 top/left
修改位置。DocumentFragment
)。避免强制同步布局:
// 错误示例:强制同步布局
const width = element.offsetWidth; // 触发重排
element.style.width = width + 10 + 'px';
回答:
浏览器出于安全考虑,实施了 同源策略(Same-Origin Policy),限制以下行为:
同源的定义:协议(http/https
)、域名、端口均相同。
CORS(跨域资源共享):
服务端设置响应头:
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST
JSONP:
<script>
标签的跨域能力,通过回调函数获取数据。代理服务器:
回答:
前端定义回调函数:
function handleResponse(data) {
console.log('数据:', data);
}
动态创建 <script>
标签:
const script = document.createElement('script');
script.src = 'https://api.example.com/data?callback=handleResponse';
document.body.appendChild(script);
服务端返回回调函数调用:
// 服务端响应内容
handleResponse({ id: 1, name: 'Alice' });
局限性:
回答:
事件循环负责协调同步任务、宏任务(MacroTask)和微任务(MicroTask)的执行顺序:
Promise.then
、MutationObserver
、queueMicrotask
。setTimeout
、setInterval
、DOM 事件、I/O 操作。执行顺序示例:
console.log(1);
setTimeout(() => console.log(2), 0);
Promise.resolve().then(() => console.log(3));
console.log(4);
// 输出顺序:1 → 4 → 3 → 2
回答:
配置文件示例:
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{ test: /\.css$/, use: ['style-loader', 'css-loader'] },
],
},
plugins: [new HtmlWebpackPlugin({ template: './src/index.html' })],
};
回答:
特性 | Loader | Plugin |
---|---|---|
功能 | 处理单个文件(如转换 Sass 为 CSS)。 | 在打包生命周期中执行更广泛的任务(如优化、资源管理)。 |
执行时机 | 在模块加载时处理文件。 | 在整个打包过程中通过钩子介入。 |
配置方式 | 在 module.rules 中定义。 | 在 plugins 数组中实例化。 |
Loader 示例:处理 CSS 文件
{
test: /\.css$/,
use: ['style-loader', 'css-loader']
}
Plugin 示例:生成 HTML 文件
new HtmlWebpackPlugin({
template: './src/index.html'
})
回答: 热模块替换(HMR)允许在不刷新页面的情况下更新模块:
webpack/hot/dev-server
)接收新模块代码并替换旧模块。关键配置:
devServer: {
hot: true, // 启用 HMR
},
plugins: [new webpack.HotModuleReplacementPlugin()]