JavaScript
Articles

Why Angular Uses NgModules With ESModules

一月份面试时遇到位面试官,他问道:为什么 Angular 至今依然坚持使用 NgModules 而不是 ESModules 呢?当时回答得比较肤浅,主要提到“Angular 对模块理念的重视与推崇,因而需要一套更好的机制解决模块的依赖问题,而这套机制遵循了控制反转原则并以依赖注入的方式实现,这是 ESModules 无法做到的”。

ZH

Caveats of ngVue: Using Vue2 Into AngularJS

The ngVue module allows you to use Vue components in AngularJS applications, making change detection efficient but complicated. AngularJS uses a dirty checking mechanism while VueJS uses a reactivity system. ngVue makes use of the reactivity system in the dirty checking mechanism, but has limitations in detecting changes to models. Solutions include using Vue.set() to add properties and Array.prototype.splice to mutate arrays.

EN

Unary map(parseInt) to Fix the Not-A-Number Issue

The example of numbers.map(parseInt) in JavaScript shows how the Array.prototype.map method calls the provided parseInt function once for each element in the array and converts strings into integers. However, the actual result failed and made the last two elements NaN. This is because parseInt only defines two parameters: string and radix, and the third argument in numbers.map(parseInt) is ignored. Solutions include using Lodash's unary method or Vanilla JS's unary and ary-like functions, or simply using numbers.map(str => parseInt(str, 10)).

EN

Define Static Constant Properties In JS Objects

在 Java 中在类中定义静态常量是一件非常轻松的事情;当我把这种定义转移到 JavaScript 上明显感到吃力,不过结合 ES5 API 和 ES6 特性以 TDD 方式讨论如何定义静态常量,同时讨论了常量 constant 与不可变性 immutable 之间的关系。

ZH

Hackability of DOM

DOM 属性迁移到 JavaScript 原型之后提升了 DOM 编程的可自定义能力,但却在 Safari 中出现了不同的行为,所以本文也只针对 Chrome/Firefox 浏览器通过这种可自定义能力实现一个非侵入式的 DOM 事件统计,同时意外发现另一处不同……

ZH

Chrome: DOM attributes now on the prototype

Chrome 团队为了与 Web IDL 规范保持一致,已经把 DOM 属性迁移到了原型上,带来了诸如自定义默认 DOM 行为等新特性,同时也带来了一些兼容问题……

ZH

How To Truncate Numbers In JavaScript

分析了开发抽奖游戏时遇到的一个问题:最初使用了 Math.random() 生成随机小数,然后使用 Math.ceil() 进行取整,但结果出现了问题。之后,介绍了 Math.floor()、Math.round()、parseInt() 和 Math.trunc() 等方法用于生成随机整数的不同方式,以及位逻辑运算符的作用。最后,总结了处理生成随机整数的不同方法。

ZH