Awn_pastebin

よくミスるやつ.js

Oct 27th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //////////////////////////////////////////////////////////////////////////
  2. //
  3. //@title タイトル
  4. //@description 説明
  5. //@include http://*
  6. //@require http://...
  7. //@private
  8. //
  9. //作った人: Awn(@Awn_tw)
  10. //
  11. //改定履歴
  12. //-201xxxxx(ver 1.0.0): 新規作成
  13. //
  14. //諸注意
  15. //-ご利用は自己責任でお願いします。
  16. //-スクリプトは予告なく修正または廃止されることがあります。
  17. //-コンソールでしか動かない、と思います。
  18. //-悪用は厳禁です。
  19. //-改造改良改悪はご自由にどうぞ。
  20. //
  21. //////////////////////////////////////////////////////////////////////////
  22.  
  23. /********************************************************/
  24. /*[凡例]
  25. /********************************************************/
  26.  
  27. /*--------------------------------*/
  28. //大見出し
  29. /*--------------------------------*/
  30.  
  31. /*----------------*/
  32. //中見出し
  33. /*----------------*/
  34.  
  35. /* 一行見出し */
  36.  
  37. //小見出し
  38.  
  39. //TODO:
  40.  
  41. /********************************************************/
  42.  
  43.  
  44. /*--------------------------------*/
  45. // 変数とエラーとtypeof
  46. /*--------------------------------*/
  47. //宣言:
  48. var a = 1;
  49. var b;
  50. //var c;
  51.  
  52. //呼び出し:
  53. a;//=> 1
  54. b;//=> undefined
  55. c;//=> Uncaught ReferenceError: c is not defined(…)
  56.  
  57. //typeof:
  58. typeof(a);//=> "number"
  59. typeof(b);//=> "undefined"
  60. typeof(c);//=> "undefined"
  61. typeof(1);//=> "number"
  62. typeof("");//=> "string"
  63. typeof([]);//=> "object"
  64. typeof({});//=> "object"
  65. typeof(function(){});//=> "function"
  66. typeof((function(){})());//=> "undefined"
  67. typeof((function(){return 1;})());//=> "number"
  68. typeof((function(){return function(){};})());//=> "function"
  69. typeof(0);//=> "number"
  70. typeof(-0);//=> "number"
  71. typeof(+0);//=> "number"
  72. typeof(true);//=> "boolean"
  73. typeof(false);//=> "boolean"
  74. typeof(undefined);//=> "undefined"
  75. typeof(null);//=> "object"
  76. typeof(/abc/);//=> "object"
  77.  
  78.  
  79.  
  80. /*--------------------------------*/
  81. // よくやるミス
  82. /*--------------------------------*/
  83.  
  84. /*----------------*/
  85. //スプリットをチェーンでつなぎまくって値を取得しようとする
  86. /*----------------*/
  87. "ab-c,de-fg,hi-jk-l".split(",")[1].split("-")[2].split("")[0];//=> Uncaught TypeError: Cannot read property 'split' of undefined(…)
  88.     /*
  89.         長すぎてどこが怪しいのかわからないが、
  90.         undefinedをsplitしてエラーが起きていることはわかる。
  91.     */
  92.  
  93. //短くしてみるとよく分かる。
  94. "ab-c,de-fg,hi-jk-l";//=> "ab-c,de-fg,hi-jk-l"
  95. "ab-c,de-fg,hi-jk-l".split(",");//=> ["ab-c", "de-fg", "hi-jk-l"]
  96. "ab-c,de-fg,hi-jk-l".split(",")[1];//=> "de-fg"
  97. "ab-c,de-fg,hi-jk-l".split(",")[1].split("-");//=> ["de", "fg"]
  98. "ab-c,de-fg,hi-jk-l".split(",")[1].split("-")[2];//=> undefined
  99.     /*
  100.         ↑なんともしょぼいミスだが、配列のrange外の値を取り出してundefinedが返ってきている
  101.     */
  102. "ab-c,de-fg,hi-jk-l".split(",")[1].split("-")[2].split("");//=> Uncaught TypeError: Cannot read property 'split' of undefined(…)
  103.     /*
  104.         ↑undefinedをsplitしているので、Uncaught TypeError
  105.         splitは"string"のprototype method
  106.     */
  107. "ab-c,de-fg,hi-jk-l".split(",")[1].split("-")[2].split("")[0];//=> Uncaught TypeError: Cannot read property 'split' of undefined(…)
  108.     /*
  109.         ↑前段でこけているのでそもそも要素へのアクセス(~~~[0])は実行されていない
  110.     */
  111.  
  112.  
  113. /*----------------*/
  114. //類似、というか自分がはまったミス: jQueryと絡めて失敗してしまうケース
  115. /*----------------*/
  116. $("#mygreatid").text().split("")[0].split("")[1].split("")[2];//=> Uncaught TypeError: Cannot read property 'split' of undefined(…)
  117.     //#mygreatidが見つからない -> text()したら"" -> ""をsplit("")したら[] -> [][0]はundefind -> undefindをsplitしたら Uncaught TypeError <- ここで止まる
  118.     /*
  119.         根本原因:そもそも、要素が見つかっていないのに処理をしてしまったこと
  120.             =>解決策:要素が見つかったかどうか事前にチェックする
  121.             $("#mygreatid")は
  122.                 ・要素が存在すれば $("#mygreatid").length > 0 === true
  123.                 ・要素が存在しなければ $("#mygreatid").length > 0 === false
  124.             であるので、これを使わない手はない。というか使おう。
  125.             こうすればなんと、try{}catch(e){}する必要がない(はず?)
  126.     */
  127.  
  128. /* 結論: */
  129. //ダメな例
  130. var foo = $("#mygreatid").text().split("")[0].split("")[1].split("")[2];
  131.  
  132. //良い例
  133. var foo = $("#mygreatid");
  134. foo = foo.length > 0 ? foo.text().split("")[0].split("")[1].split("")[2] : undefined;
  135.     /*
  136.         中身を確認してから処理しましょう。
  137.     */
Add Comment
Please, Sign In to add comment