「JavaScript」の版間の差分
 (→データ型)  | 
				 (→データ型)  | 
				||
| (同じ利用者による、間の15版が非表示) | |||
| 1行目: | 1行目: | ||
{{JavaScript}}  | |||
ちょっと離れると忘れちゃうんで書きなぐりでメモ  | |||
* CAAT(高性能JavaScriptアニメーションフレームワーク) - https://hypertolosana.github.io/caat/  | * CAAT(高性能JavaScriptアニメーションフレームワーク) - https://hypertolosana.github.io/caat/  | ||
| 6行目: | 7行目: | ||
== コメントの書き方 ==  | == コメントの書き方 ==  | ||
<  | <pre class="brush:javascript;">  | ||
//  // 以降がコメント  | //  // 以降がコメント  | ||
| 13行目: | 14行目: | ||
コメント  | コメント  | ||
*/  | */  | ||
</  | </pre>  | ||
==データ型==  | ==データ型==  | ||
| 19行目: | 20行目: | ||
* Undefined … 未定義の値を表す。初期化されていないデータは、この Undefined型を持つ。意図的に Undefined 型の値を仕様したい場合は、 Undefined を使用する。  | * Undefined … 未定義の値を表す。初期化されていないデータは、この Undefined型を持つ。意図的に Undefined 型の値を仕様したい場合は、 Undefined を使用する。  | ||
* 真偽値型(boolean) … 条件が成り立つか成り立たないかを表す。真偽値型の値は、true(真、条件が成り立つ)と false (偽、条件が成り立たない)の2つのみ  | * 真偽値型(boolean) … 条件が成り立つか成り立たないかを表す。真偽値型の値は、true(真、条件が成り立つ)と false (偽、条件が成り立たない)の2つのみ  | ||
* 数値型 (number) …   | * 数値型 (number) … 有限の少数を表す。javascriptで数値を表す場合は、主にこの型を使用する。表現できる値の範囲が限られている。64 ビット倍精度浮動小数点数のフォーマットである IEEE 754 にしたがって実装されていて、 ±2^53 −1 までの整数は正確に表現できる。  | ||
* 長整数型 (bigint)… 上下限のない整数値を表す。  | * 長整数型 (bigint)… 上下限のない整数値を表す。  | ||
* 文字型 (string) … 文字列、つまり文字の並びを表す。  | * 文字型 (string) … 文字列、つまり文字の並びを表す。  | ||
| 25行目: | 26行目: | ||
* オブジェクト型 (object) … 下部構造を持つデータを表す。データやそのデータに対する処理を内部的に持つことが出来る。  | * オブジェクト型 (object) … 下部構造を持つデータを表す。データやそのデータに対する処理を内部的に持つことが出来る。  | ||
** 非オブジェクト型の NUll, Undefined, 真偽値型(boolean), 数値型 (number), 長整数型 (bigint), 文字型 (string), シンボル型(symbol)   | ** 非オブジェクト型の NUll, Undefined, 真偽値型(boolean), 数値型 (number), 長整数型 (bigint), 文字型 (string), シンボル型(symbol) の値は"プリミティブ"と呼ばれる。(内部的な構造が無い代わりに演算子を用いた処理の効率がよく早い。)<< LISP でいうところの Atom に近いかも。  | ||
== データ型の判別 ==  | |||
* 型を判定するには typeof 演算子を使用する。 [https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/typeof MDN – typeof]  | |||
 console.log(typeof 12.0); // 返り値: "number"  | |||
 console.log(typeof -4.2e4);// 返り値: "number"   | |||
 console.log(typeof 'blubber');  // 返り値: "string"  | |||
 console.log(typeof true); // 返り値: "boolean"  | |||
 console.log(typeof undeclaredVariable); // 返り値: "undefined"  | |||
 console.log(typeof null); // 返り値: "object"  | |||
* typeof演算子だと null やオブジェクトを詳しく判別できないので、Object.prototype.toString を使う方法もある。[https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/toString MDN -Object.prototype.toString]  | |||
 const o = new Object();  | |||
 console.log(o.toString()); // [object Object] を返す  | |||
==変数の宣言==  | ==変数の宣言==  | ||
<  | <pre class="brush:javascript;">  | ||
// 変数の宣言  | // 変数の宣言  | ||
var message = "Hello World, Again!"; // 文字列  | var message = "Hello World, Again!"; // 文字列  | ||
| 48行目: | 64行目: | ||
var y = x++;  | var y = x++;  | ||
alert(y);  | alert(y);  | ||
</  | </pre>  | ||
==真・偽==  | ==真・偽==  | ||
<  | |||
<pre class="brush:autolisp;">  | |||
  // true / false 真偽  |   // true / false 真偽  | ||
  if (x == 5) { // }  |   if (x == 5) { // }  | ||
| 61行目: | 78行目: | ||
  // true/false = そのまま  |   // true/false = そのまま  | ||
  // object/配列 = true  |   // object/配列 = true  | ||
</  | </pre>  | ||
==論理演算子==  | ==論理演算子==  | ||
論理演算子は以下の3つ。  | |||
* AND (理論積)… &&    | |||
* OR (理論和) … ||  | |||
* NOT(論理否定)… !  | |||
<pre class="brush:javascript;">  | |||
  if ((x > 2) && (x < 5)) {  |   if ((x > 2) && (x < 5)) {  | ||
      // x が2より小さい "か" 5より小さい ならなんらかの処理  |       // x が2より小さい "か" 5より小さい ならなんらかの処理  | ||
| 102行目: | 123行目: | ||
  } while (x < 10);  |   } while (x < 10);  | ||
</  | </pre>  | ||
==三項演算子==  | ==三項演算子==  | ||
<  | <pre class="brush:javascript;">  | ||
// if 文の省略記法  | // if 文の省略記法  | ||
// 三項演算子  | // 三項演算子  | ||
| 133行目: | 154行目: | ||
document.write( arr[exist?0:1] );  | document.write( arr[exist?0:1] );  | ||
</  | </pre>  | ||
==タイムアウト==  | ==タイムアウト==  | ||
<  | <pre class="brush:javascript;">  | ||
  // setTimeout: 前回の処理が終わったかどうかを考慮する  |   // setTimeout: 前回の処理が終わったかどうかを考慮する  | ||
  // setInterval: 考慮しない  |   // setInterval: 考慮しない  | ||
| 157行目: | 178行目: | ||
  plusOne();  |   plusOne();  | ||
</  | </pre>  | ||
==型も値の比較==  | ==型も値の比較==  | ||
<  | <pre class="brush:javascript;">  | ||
  // 厳密な比較演算子 ===  !== は型変換しないで比較、  |   // 厳密な比較演算子 ===  !== は型変換しないで比較、  | ||
  // ==  != は型変換して比較  |   // ==  != は型変換して比較  | ||
| 191行目: | 212行目: | ||
  alert(a == b);// true  |   alert(a == b);// true  | ||
  alert(a === b);// false  |   alert(a === b);// false  | ||
</  | </pre>  | ||
==即時関数・無名関数==  | ==即時関数・無名関数==  | ||
<  | <pre class="brush:javascript;">  | ||
  // 即時関数、無名関数  |   // 即時関数、無名関数  | ||
  (function() {  |   (function() {  | ||
     console.log("hello world");  |      console.log("hello world");  | ||
  })();  |   })();  | ||
</  | |||
</pre>  | |||
==乱数==  | ==乱数==  | ||
<  | <pre class="brush:javascript;">  | ||
  // 乱数  |   // 乱数  | ||
  // Math.random()  |   // Math.random()  | ||
| 210行目: | 232行目: | ||
  // 1から6まで  |   // 1から6まで  | ||
  var y = Math.floor(Math.random() * 6) + 1;  |   var y = Math.floor(Math.random() * 6) + 1;  | ||
</  | </pre>  | ||
== JavaScript で tarai 関数 ==  | |||
<pre class="brush:javascript;">  | |||
// 素直に作る  | |||
function tarai_n(x,y,z) {  | |||
  if(x <= y)  | |||
    return y;  | |||
    return tarai_n(tarai_n(x-1,y,z),tarai_n(y-1,z,x),tarai_n(z-1,x,y));  | |||
}  | |||
// メモ化して高速化  | |||
function memoize(func) {  | |||
    var table = {};  | |||
    function _memo_func() {  | |||
        var key = "";  | |||
        for (var i = 0; i < arguments.length; i++) key += arguments[i] + ",";  | |||
            if (!table[key]) {  | |||
                table[key] = func.apply(null, arguments);  | |||
            }  | |||
        return table[key];  | |||
    }  | |||
    return _memo_func;  | |||
}  | |||
tarai = memoize(tarai);  | |||
// 遅延評価して高速化(クロージャ利用)  | |||
function tarai(x, y, z) {  | |||
    if (x <= y) return y;  | |||
    var zz = z();  | |||
    return tarai(tarai(x - 1, y,  function() { return zz; }),  | |||
                 tarai(y - 1, zz, function() { return x; }),  | |||
                 function() { return tarai(zz - 1, x, function() { return y; }); });  | |||
}  | |||
</pre>  | |||
[[Category:javascript]]  | [[Category:javascript]]  | ||
2021年8月15日 (日) 09:40時点における最新版
ちょっと離れると忘れちゃうんで書きなぐりでメモ
- CAAT(高性能JavaScriptアニメーションフレームワーク) - https://hypertolosana.github.io/caat/
 - イベントハンドラ(javascript)
 - MDN Web doc
 
コメントの書き方
// // 以降がコメント /* 複数行にまたがった コメント */
データ型
- NUll … 存在しないオブジェクトを表す。オブジェクトが存在しないことを明示的に示すために使用する。NULL型の値は Nullのみ。
 - Undefined … 未定義の値を表す。初期化されていないデータは、この Undefined型を持つ。意図的に Undefined 型の値を仕様したい場合は、 Undefined を使用する。
 - 真偽値型(boolean) … 条件が成り立つか成り立たないかを表す。真偽値型の値は、true(真、条件が成り立つ)と false (偽、条件が成り立たない)の2つのみ
 - 数値型 (number) … 有限の少数を表す。javascriptで数値を表す場合は、主にこの型を使用する。表現できる値の範囲が限られている。64 ビット倍精度浮動小数点数のフォーマットである IEEE 754 にしたがって実装されていて、 ±2^53 −1 までの整数は正確に表現できる。
 - 長整数型 (bigint)… 上下限のない整数値を表す。
 - 文字型 (string) … 文字列、つまり文字の並びを表す。
 - シンボル型(symbol) … 同じ値を複数作ることができない一意の値を表す。基本的にはシステムによって作成され、開発者が作成することはほぼ無い。
 - オブジェクト型 (object) … 下部構造を持つデータを表す。データやそのデータに対する処理を内部的に持つことが出来る。
 
- 非オブジェクト型の NUll, Undefined, 真偽値型(boolean), 数値型 (number), 長整数型 (bigint), 文字型 (string), シンボル型(symbol) の値は"プリミティブ"と呼ばれる。(内部的な構造が無い代わりに演算子を用いた処理の効率がよく早い。)<< LISP でいうところの Atom に近いかも。
 
データ型の判別
- 型を判定するには typeof 演算子を使用する。 MDN – typeof
 
console.log(typeof 12.0); // 返り値: "number" console.log(typeof -4.2e4);// 返り値: "number" console.log(typeof 'blubber'); // 返り値: "string" console.log(typeof true); // 返り値: "boolean" console.log(typeof undeclaredVariable); // 返り値: "undefined" console.log(typeof null); // 返り値: "object"
- typeof演算子だと null やオブジェクトを詳しく判別できないので、Object.prototype.toString を使う方法もある。MDN -Object.prototype.toString
 
const o = new Object(); console.log(o.toString()); // [object Object] を返す
変数の宣言
// 変数の宣言 var message = "Hello World, Again!"; // 文字列 var d1 = -5.5; // 数値 var d2 = false; // 真偽 var d3 = undefined; // 定義されていない var d4 = null; // 何もない var d5 = []; // 空の配列 var d5[0] = "a"; // 1つ目の配列要素に "a" を追加。 // アラート表示 alert(message); // 単項演算子 ++ -- var x = 0; var y = x++; alert(y);
真・偽
 // true / false 真偽
 if (x == 5) { // }
 // 文字列 : 空文字だとfalse
 // 数値 : 0かNaNだとfalse
 // undefined = false
 // null = false
 // true/false = そのまま
 // object/配列 = true
論理演算子
論理演算子は以下の3つ。
- AND (理論積)… &&
 - OR (理論和) … ||
 - NOT(論理否定)… !
 
 if ((x > 2) && (x < 5)) {
     // x が2より小さい "か" 5より小さい ならなんらかの処理
 }
 if ((x > 2) || !(x < 5)) {
     // x が2より小さい "または" 5より小さくない ならなんらかの処理
 }
 if !(x < 5) {
     // x が 5より"小さくければ"なんらかの処理
 }
 // switch
 var x = 0;
 switch (x) {
   case 0:
   case 1:
          // 0 1 は同じ処理
          break;
   case 2:
          //
          break;
   default:
          //
          break;
 }
 // ループ(while)
 while (x < 10) {
    // なにかの処理。条件に合わなかったら実行されない
 }
        
 do {
    // なにかの処理。一度は実行される。
 } while (x < 10);
三項演算子
// if 文の省略記法
// 三項演算子
// (条件) ? (真だった場合の値) : (偽だった場合の値)
if( a>b )
  a=b;
else
  a=c;
// 上のif文の省略記法。
(a>b)? a=b : a=c ;
// 例
var sex = '';
if (sex == '男性') {
    price = 5000;
} else {
    price = 3000;
}
// 三項演算子にすると
price = (sex == '男性') ? 5000 : 3000;
// 三項演算子は式なので
var arr = ["○","×"];
var exist = true;
document.write( arr[exist?0:1] );
タイムアウト
 // setTimeout: 前回の処理が終わったかどうかを考慮する
 // setInterval: 考慮しない
 //setInterval(function() {
     // 1秒以上の処理 → クラッシュ
 //}, 1000);
 var i = 0;
 /*
    setInterval(function() {
      console.log(++i);
    }, 1000);
 */
 function plusOne() {
    console.log(++i);
    setTimeout(plusOne, 1000);
 }
 plusOne();
型も値の比較
 // 厳密な比較演算子 ===  !== は型変換しないで比較、
 // ==  != は型変換して比較
 var a = 5;
 var b = "5";
 console.log(a !== b);
 // 数値とブール値
 alert(1 == true);// true
 alert(0 == false);// true
 alert(1 === true);// false
 alert(0 === false);// false
 // 数値と文字列
 var a = 10;
 var b = "10";
 alert(a == b);// true
 alert(a === b);// false
 // オブジェクト
 var a = [1,2,3];
 var b = a;
 alert(a == b);// true 
 alert(a == [1,2,3]);// false
 // 文字列と String オブジェクト
 var a = "abc";
 var b = new String("abc");
 alert(a == b);// true
 alert(a === b);// false
即時関数・無名関数
 // 即時関数、無名関数
 (function() {
    console.log("hello world");
 })();
乱数
// 乱数 // Math.random() // 0からnまでの整数値 // var x = Math.floor(Math.random() * (n+1)); // 1から6まで var y = Math.floor(Math.random() * 6) + 1;
JavaScript で tarai 関数
// 素直に作る
function tarai_n(x,y,z) {
  if(x <= y)
    return y;
    return tarai_n(tarai_n(x-1,y,z),tarai_n(y-1,z,x),tarai_n(z-1,x,y));
}
// メモ化して高速化
function memoize(func) {
    var table = {};
    function _memo_func() {
        var key = "";
        for (var i = 0; i < arguments.length; i++) key += arguments[i] + ",";
            if (!table[key]) {
                table[key] = func.apply(null, arguments);
            }
        return table[key];
    }
    return _memo_func;
}
tarai = memoize(tarai);
// 遅延評価して高速化(クロージャ利用)
function tarai(x, y, z) {
    if (x <= y) return y;
    var zz = z();
    return tarai(tarai(x - 1, y,  function() { return zz; }),
                 tarai(y - 1, zz, function() { return x; }),
                 function() { return tarai(zz - 1, x, function() { return y; }); });
}