.npmrcの設定はハイフン(-)かドット(.)か
デフォルトはハイフンです。なのになぜかドットでも npm init
したときにはきちんと参照されます。
そのためかドットを使った .npmrc
の紹介記事が散見されます。
<追記>
npm 公式でも ハイフンとドットが混在していました。統一したほうが良いのでは…?
</追記>
$ # npm config set init-license MIT ホントはこっち $ npm config set init.license MIT
# .npmrc # init-license=MIT init.license=MIT
ハイフン・ドット両方ある場合には後のものが優先されます。
ドットを使った場合の問題点
npm init
以外では両者は別のものとして扱われます。
以下はドットのみ設定した場合です。
ターミナルからは npm config get
で設定を確認できます。
$ npm config get init.license MIT $ npm config get init-license ISC
ハイフンを使った init-license
の方はデフォルトの設定が参照されていることが分かります。
また、.npmrc
の設定はスクリプトから読み込むこともできますが、
こちらもドットがハイフンに変換されずにそのままになります。
.npmrc の読み込み方は
~/.npm-init.js
からはconfig.sources
- 他のスクリプトからは
require("npm").load((err, npm)=> console.log(npm.config.sources))
で出力結果はこのようになります。
config.sources = ... user: { ... data: { 'init.license': 'MIT', // ドットで設定したよ 'init-version': '0.0.1', // ハイフンで設定したよ color: 'always', ... } }
そのためユーザー設定を読み取る際にはドットとハイフン両方対応する必要があります。
実際に問題になるケースは限られると思いますが、 わざわざドットを使う理由がないのでハイフンを使いましょう。
多次元配列の再帰の記述
追記: 2018年現在は一度flattenしてから処理するのが良いと思います。
記述の方法が、for-loopで書くと1通りだが、イテレートメソッドだと2通りある。
どちらでもよいのだけれど、混ざるので整理。
for-loopとイテレートは完全に同じではないが、その点は割愛します。
例えば以下のような配列をすべて処理したい場合
var array = [1, 2 , [3, [4], 5], 6] var fn = console.log.bind(console)
for-loopでの再帰。(コード1)
function forLoop(array, fn) { for (var i=0; i<array.length; i++) { Array.isArray(array[i]) ? forLoop(array[i], fn) : fn(array[i]) } }
イテレートでの再帰(コード2)
普通再帰はその関数自体を呼び出すので、その例に倣うとこちらの書き方になる。for-loopと同じ構造。
function eachOuter(array, fn) { array.forEach(function(e) { Array.isArray(e)? eachOuter(e, fn) : fn(e) }) }
しかし、イテレートの再帰はこうも書ける(コード3)
function eachInner(array, fn) { array.forEach(function internal(e) { Array.isArray(e)? e.forEach(internal) : fn(e) }) }
配列を直接イテレートするならコード3の書き方になる。(コード4)
// fnは外部で宣言してもよいし、直接書いてもよい array.forEach(function internal(e) { Array.isArray(e)? e.forEach(internal) : fn(e) })
こうしてみると、どちらをベースにするかで書き方が変わるようだ。
- コード1をベース => コード2
- コード4をベース => コード3
img.width, img.height と img.setAttribute("width"/ "height"
JSからサイズを設定する場合
OK
img.width = "100" img.setAttribute("width", "100") img.setAttribute("width", "100px")
NG
img.width = "100px"
といっても、本来<image>
のwidth, heightに入れられるのは数値なので
img.width = "100" img.width = Number("100") img.width = 100
と なり、単位を付けない書き方が一番良い。
px以外の単位(パーセントなど)を指定するならCSSを用いる。
ネイティブメソッドを判別する互換性
setTimeout, setIntervalを乗っ取って爆速にする - 素人がプログラミングを勉強…を踏まえて
"prototype" in Array
はすべてtrue"prototype" in Array.prototype.push
はすべてfalse"prototype" in function(){}
はすべてtrue"prototype" in {native dom func}
は互換性が無い(下の表参照)
つまり
browser | "prototype" in alert //native |
---|---|
Windows 8.1 Internet Explorer 11.0 Desktop | T |
Windows 8 Opera 12.16 | F |
OS X Mavericks Safari 7.0 | T |
Windows 8 Firefox 25.0 | F |
Windows 8 Chrome 31.0 | F |
Google Nexus 7 Android Browser | T |
Samsung Galaxy S III Android Browser | T |
iPhone 5S Mobile Safari | F |
iPad 3rd (7.0) Mobile Safari | F |
(JavaScriptで使われる)数学の英語 数字編
- digit(s)
- 0-9の数字(0は除かれることも)
- notation
- 表記
- integer
- 整数
- decimal
- 小数 / 10進数
- decimal point
- 小数点
- fixed-point number
- 固定小数点数
- floating-point number
- 浮動小数点数
- precision
- 精度
- exponent
- 指数
- exponential notation
- 指数表記
- radix
- base number
- 基数
- decimal
- 10進数
- hexadecimal
- 16進数
- octal
- 8進数
- binary
- 2進数
- finite
- 有限数
- infinite
- 無限数
- positive
- 正の
- negative
- 負の
「数字編」としたが、「数学編」はあまりにも分量が無いので投稿しない予定・・・´ω`)