惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
博客园_首页
雷峰网
雷峰网
V
V2EX
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - Franky
月光博客
月光博客
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
T
Tailwind CSS Blog
小众软件
小众软件
博客园 - 叶小钗
美团技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
IT之家
IT之家
MyScale Blog
MyScale Blog
Blog — PlanetScale
Blog — PlanetScale
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
人人都是产品经理
人人都是产品经理
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

Surmon.me

一人有限集团 你就是不敢 创造力是温柔的谎言 人类正在退出人类 AI 代替不了这样的你 脉冲点火背后的架构设计 基于 Cloudflare 生态的 AI Agent 实现 NodePress 支持用户登录了 从统计学习到通用智能 2025 投资报告:走慢的路 无依之地 会杀人的菩萨 无我不是共识 文化的积重与偏见 当下即安 科学的尽头是态度 无我不是 Egoless 信仰不因恐惧而存在 世间无解的矛与盾 先别急着做些什么 佛不需要你的皈依 真理的幻觉 两扇大门 造心里的浮屠 自胜者强 逻辑与智慧 真的相 快乐的秘密 只需愿意 是名体验
JavaScript 代码片段合集
Surmon · 2017-07-03 · via Surmon.me

原创

1. 返回一个已排序的可循环对象

        
        

123456

var dist = {'a': ['A', 3], 'c': ['B', 4], 'b': ['C', 2], 'e': ['A', 2.7], 'd': ['B', 1]} Object.keys(dist).reduce((r, i) => { if (r[dist[i][1]] == undefined || r[dist[i][1]] > i) r[dist[i][1]] = i return r }, {})

2. 最简单的方式实现依赖注入

        
        

123456789101112131415161718192021

/** * Constructor DependencyInjector * @param {Object} - object with dependencies */ class DI { constructor(dependency) { this.dependency = dependency } inject(func) { let deps = /^[^(]+\(([^)]+)/.exec(func.toString()) // 构建参数绑定数组 deps = !deps ? [] : deps[1].split(/\s?,\s?/).map(dep => this.dependency[dep]) return function () { return func.apply(this, deps) } } }

3. 实现一个字符串形式的 IP 地址,转为 INT 整型保存,及逆转方法

        
        

123456789101112131415161718192021222324

const ip2int = function(ip) { var num = 0 ip = ip.split(".") num = Number(ip[0]) * 256 * 256 * 256 + Number(ip[1]) * 256 * 256 + Number(ip[2]) * 256 + Number(ip[3]) num = num >>> 0 return num } /** * 数字转ip * @param num * @returns {string|*} * @private */ /*global _int2ip(num: number) */ const int2iP = function(num) { var str var tt = new Array() tt[0] = (num >>> 24) >>> 0 tt[1] = ((num << 8) >>> 24) >>> 0 tt[2] = (num << 16) >>> 24 tt[3] = (num << 24) >>> 24 str = String(tt[0]) + "." + String(tt[1]) + "." + String(tt[2]) + "." + String(tt[3]) return str }