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
- 負の
「数字編」としたが、「数学編」はあまりにも分量が無いので投稿しない予定・・・´ω`)
はてなブログのテーマのライセンスに、GPLが選べるようになりました
IEで「オブジェクトを指定してください」のエラー
エラーメッセージを言い換えると分かった。
「オブジェクトを指定してください」
「オブジェクトが指定されていないよ」
「オブジェクトが無いよ」
「オブジェクトがundefined
だよ」
原因
3通りあるようだ
原因1 古いIEと文字コード
IEの謎エラー対策(1) 「オブジェクトを指定して下さい」が外部ソースの場合 - 趣味の延長線 の通り、charsetを指定する。
原因2 関数が未定義
例
var hoge hoge(1) // "オブジェクトを指定してください。"
原因3 関数の後にセミコロン忘れ
(function(){})() (function(){})()
戻り値の無い関数はundefined
を返す。
分かりやすいようにfunction
をalert
に置き換えて整理すると、
(alert)() // =undefined (alert)()
そしてこうなる。
undefined(alert)()
この時、
undefined()
という式を実行しようとする。原因2と同じように、関数ではないものを実行しようとしているのでエラー。
余談、エラーメッセージについて
obj.propの形の関数だと少し分かりにくい。
Math.foo(1)
"オブジェクトは 'foo' プロパティまたはメソッドをサポートしていません。"
[Math.foo].map(function(e){ return e(1) })
"オブジェクトを指定してください。"
同じことなら、同じエラーメッセージにして欲しい。