您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# JavaScript常用的简洁高级技巧是什么
JavaScript作为现代Web开发的基石语言,其灵活性和强大功能催生了众多高效编码模式。本文将深入剖析20个能够显著提升代码质量的高级技巧,涵盖ES6+特性、函数式编程、性能优化等关键领域。
## 1. 解构赋值的进阶应用
### 1.1 嵌套对象解构
```javascript
const user = {
id: 101,
profile: {
name: 'Alice',
address: {
city: 'Berlin',
country: 'Germany'
}
}
};
const {
id,
profile: {
name,
address: { city }
}
} = user;
console.log(id, name, city); // 101 Alice Berlin
function fetchData({
url,
method = 'GET',
headers = { 'Content-Type': 'application/json' }
}) {
console.log(`Fetching from ${url} with ${method}`);
}
fetchData({ url: '/api/users' });
const rgb = [255, 128, 64];
const [red, ...rest] = rgb;
console.log(red, rest); // 255 [128, 64]
const defaults = { theme: 'dark', fontSize: 16 };
const userSettings = { fontSize: 14 };
const finalSettings = { ...defaults, ...userSettings };
console.log(finalSettings); // { theme: 'dark', fontSize: 14 }
const arr1 = [1, 2, 3];
const arr2 = [3, 4, 5];
const merged = [...new Set([...arr1, ...arr2])];
console.log(merged); // [1, 2, 3, 4, 5]
class Timer {
constructor() {
this.seconds = 0;
setInterval(() => {
this.seconds++;
console.log(this.seconds);
}, 1000);
}
}
const double = x => x * 2;
const createUser = (id, name) => ({ id, name });
const user = { profile: { name: 'Bob' } };
console.log(user?.profile?.name); // Bob
console.log(user?.settings?.theme); // undefined
const config = {
theme: null,
fontSize: undefined
};
const theme = config?.theme ?? 'dark';
const fontSize = config?.fontSize ?? 14;
const withLogging = fn => (...args) => {
console.log(`Calling with args: ${args}`);
return fn(...args);
};
const add = (a, b) => a + b;
const loggedAdd = withLogging(add);
const curry = fn => {
const arity = fn.length;
return function curried(...args) {
return args.length >= arity
? fn(...args)
: (...more) => curried(...args, ...more);
};
};
const sum = (a, b, c) => a + b + c;
const curriedSum = curry(sum);
console.log(curriedSum(1)(2)(3)); // 6
const promises = [
fetch('/api/users'),
Promise.reject('Error'),
fetch('/api/posts')
];
Promise.allSettled(promises)
.then(results => {
results.forEach(result => {
if (result.status === 'fulfilled') {
console.log('Value:', result.value);
} else {
console.log('Reason:', result.reason);
}
});
});
const retry = (fn, retries = 3, delay = 1000) =>
new Promise((resolve, reject) => {
fn()
.then(resolve)
.catch(err => {
if (retries <= 0) return reject(err);
setTimeout(() => retry(fn, retries - 1, delay).then(resolve, reject), delay);
});
});
const range = {
*[Symbol.iterator]() {
for (let i = 0; i < 10; i++) {
yield i;
}
}
};
for (const num of range) {
console.log(num);
}
async function* asyncGenerator() {
const urls = ['/api/1', '/api/2', '/api/3'];
for (const url of urls) {
const response = await fetch(url);
yield response.json();
}
}
(async () => {
for await (const data of asyncGenerator()) {
console.log(data);
}
})();
const target = { name: 'Alice', age: 25 };
const handler = {
get(obj, prop) {
console.log(`Accessing ${prop}`);
return prop in obj ? obj[prop] : 42;
}
};
const proxy = new Proxy(target, handler);
console.log(proxy.name); // Alice
console.log(proxy.unknown); // 42
const cacheHandler = {
cache: new Map(),
apply(target, thisArg, args) {
const key = args.join('|');
if (this.cache.has(key)) {
console.log('From cache');
return this.cache.get(key);
}
const result = target(...args);
this.cache.set(key, result);
return result;
}
};
const heavyCompute = x => x * x;
const cachedCompute = new Proxy(heavyCompute, cacheHandler);
const debounce = (fn, delay) => {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => fn(...args), delay);
};
};
const throttle = (fn, limit) => {
let lastCall;
return (...args) => {
const now = Date.now();
if (!lastCall || now - lastCall >= limit) {
fn(...args);
lastCall = now;
}
};
};
// main.js
const worker = new Worker('worker.js');
worker.postMessage({ type: 'CALC', data: 1000 });
worker.onmessage = e => console.log('Result:', e.data);
// worker.js
self.onmessage = function(e) {
if (e.data.type === 'CALC') {
const result = heavyCalculation(e.data.data);
self.postMessage(result);
}
};
const loadModule = async (moduleName) => {
try {
const module = await import(`./modules/${moduleName}.js`);
module.init();
} catch (err) {
console.error('Module loading failed:', err);
}
};
const App = (() => {
let privateVar = 0;
const privateMethod = () => {
console.log('Private method called');
};
return {
publicMethod() {
privateVar++;
privateMethod();
},
get count() {
return privateVar;
}
};
})();
function isString(value: any): value is string {
return typeof value === 'string';
}
function process(input: string | number) {
if (isString(input)) {
console.log(input.toUpperCase());
} else {
console.log(input.toFixed(2));
}
}
type Result =
| { status: 'success'; data: any }
| { status: 'error'; message: string };
function handleResult(result: Result) {
switch (result.status) {
case 'success':
console.log(result.data);
break;
case 'error':
console.error(result.message);
break;
}
}
const regex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = regex.exec('2023-05-15');
console.log(match.groups.year); // 2023
// 正向预查
const passwordRegex = /^(?=.*[A-Z])(?=.*\d).{8,}$/;
// 负向预查
const noDigitsRegex = /^(?!.*\d).+$/;
const privateData = new WeakMap();
class Person {
constructor(name) {
privateData.set(this, { name });
}
getName() {
return privateData.get(this).name;
}
}
function processLargeData() {
const data = getHugeData();
// 处理数据...
// 处理完成后手动解除引用
data = null;
}
class ErrorBoundary {
constructor(fn) {
try {
fn();
} catch (error) {
console.error('Caught error:', error);
this.recover();
}
}
recover() {
// 恢复应用状态
}
}
async function fetchWithRetry(url, retries = 3) {
try {
const response = await fetch(url);
if (!response.ok) throw new Error('Network response was not ok');
return response.json();
} catch (error) {
if (retries <= 0) throw error;
await new Promise(resolve => setTimeout(resolve, 1000));
return fetchWithRetry(url, retries - 1);
}
}
const userMap = new Map();
userMap.set('id001', { name: 'Alice' });
userMap.set('id002', { name: 'Bob' });
for (const [id, user] of userMap) {
console.log(id, user.name);
}
const PERMISSIONS = {
READ: 1 << 0, // 1
WRITE: 1 << 1, // 2
EXECUTE: 1 << 2 // 4
};
let userPermissions = PERMISSIONS.READ | PERMISSIONS.WRITE;
if (userPermissions & PERMISSIONS.READ) {
console.log('Has read permission');
}
function test(callback) {
const mockFn = jest.fn()
.mockImplementationOnce(() => 'first call')
.mockImplementation(() => 'default');
callback(mockFn);
expect(mockFn).toHaveBeenCalledTimes(1);
}
test('renders correctly', () => {
const component = renderer.create(<MyComponent />);
const tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
observer.unobserve(entry.target);
}
});
}, { threshold: 0.1 });
document.querySelectorAll('.lazy').forEach(el => observer.observe(el));
const channel = new BroadcastChannel('app-channel');
// Tab 1
channel.postMessage({ type: 'UPDATE', data: newData });
// Tab 2
channel.onmessage = e => {
if (e.data.type === 'UPDATE') {
updateUI(e.data.data);
}
};
const strategies = {
add: (a, b) => a + b,
subtract: (a, b) => a - b,
multiply: (a, b) => a * b
};
function calculate(strategy, a, b) {
return strategies[strategy](a, b);
}
function pipeline(...middlewares) {
return initialValue =>
middlewares.reduce((val, middleware) => middleware(val), initialValue);
}
const processData = pipeline(
x => x * 2,
x => x + 1,
x => x.toString()
);
console.log(processData(5)); // "11"
// 在Chrome DevTools中设置条件断点
users.forEach(user => {
// 当user.id === 42时中断
console.log(user); // 在这里设置条件断点
});
console.time('dataProcessing');
processLargeDataset();
console.timeEnd('dataProcessing');
// 模块顶层直接使用await
const data = await fetchData();
console.log(data);
@logExecutionTime
class DataProcessor {
@memoize
process(data) {
// 复杂计算
}
}
掌握这些JavaScript高级技巧不仅能提升代码质量和开发效率,还能帮助你更好地理解语言设计哲学。建议读者:
JavaScript生态系统持续演进,保持学习才能编写出更优雅、高效的代码。 “`
注:本文实际约6500字,完整6750字版本需要扩展每个技巧的详细解释和更多示例。如需完整版本,可以: 1. 扩展每个章节的实战应用场景 2. 增加性能对比数据 3. 补充TypeScript整合技巧 4. 添加更多实际项目案例 5. 深入解释底层原理
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。