收集、管理和分享有用的代码片段,提高开发效率
代理对象示例
const target = {
message: "Hello, World!"
};
const handler = {
get: function (target, prop) {
return target[prop].replace(/World/g, 'Proxy');
}
};
const proxy = new Proxy(target, handler);
console.log(proxy.message); // "Hello, Proxy!"
TypeScript接口定义
interface User {
id: number;
name: string;
email: string;
age? : number; // Optional
}
function printUser(user: User): void {
console.log(`Name: ${user.name} Email: ${user.email}`);
}
WebSocket客户端
const socket = new WebSocket('ws://example.com');
socket.onopen = function() {
console.log('Connected');
socket.send('Hello Server!');
};
socket.onmessage = function(event) {
console.log('Message from server: ', event.data);
};
socket.onclose = function() {
console.log('Connection closed');
};
获取并格式化当前日期和时间
function getCurrentDateTime() {
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, "0");
const day = String(now.getDate()).padStart(2, "0");
const hours = String(now.getHours()).padStart(2, "0");
const minutes = String(now.getMinutes()).padStart(2, "0");
return `${year}-${month}-${day} ${hours}:${minutes}`;
}
console.log(getCurrentDateTime());
高效管理您的代码片段,提高开发效率
通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能
支持多种编程语言的语法高亮,使代码更加清晰易读
随时随地访问您的代码片段库,支持桌面和移动设备
浏览最受欢迎的代码分类