js 6th edition reading circle part 3
TRANSCRIPT
![Page 1: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/1.jpg)
第3回変数・演算子・文 (工事中)
開発部てるー
![Page 2: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/2.jpg)
アジェンダ
1. 変数
2. 等値演算子と同値演算子
3. for/in 文
![Page 3: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/3.jpg)
アジェンダ
1. 変数
2. 等値演算子と同値演算子
3. for/in 文
![Page 4: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/4.jpg)
変数の宣言
var x;var i, sum;
var message = ‘hello’;var a = 0, b = 0, c = 0;
![Page 5: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/5.jpg)
グローバル変数とローカル変数
● グローバル変数○ スコープ(※)はプログラム全体
● ローカル変数○ スコープ(※)は変数が宣言された関数の中に限定
※ スコープ: 変数の有効範囲
![Page 6: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/6.jpg)
グローバル変数とローカル変数
var g1 = ‘global’; // グローバル変数
function checkscope() { var local = ‘local’; // ローカル変数
g2 = ‘global’; // グローバル変数
};checkscope();
![Page 7: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/7.jpg)
同じ名前があった場合はローカルを優先
var scope = ‘global’;function checkscope() { var scope = ‘local’; return scope;};checkscope(); // local
![Page 8: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/8.jpg)
JavaScript にはブロックスコープがない
function test(obj) { for (var k = 0; k < 10; k++) { console.log(k); // 0 から 9 が出力される
} console.log(k); // 10 が出力される
}
![Page 9: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/9.jpg)
JavaScript にはブロックスコープがない
function test(obj) { if (typeof obj === ‘object’) { var j = 0; } console.log(j); // 0 or undefined が出力される
}
![Page 10: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/10.jpg)
問題
var scope = ‘global’;function f() { console.log(scope); // (1) var scope = ‘local’; console.log(scope); // (2)}f();
● (1) で出力されるのは?a. globalb. localc. 上記以外
● (2) で出力されるのは?a. globalb. localc. 上記以外
![Page 11: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/11.jpg)
アジェンダ
1. 変数
2. 等値演算子と同値演算子
3. for/in 文
![Page 12: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/12.jpg)
等値演算子と不等演算子
● 等値演算子「==」○ 左辺と右辺の値が等しいかどうかを調べる
● 不等演算子「!=」○ 左辺と右辺の値が等しくないかどうかを調べる
両方とも型変換を行いながら比較する
![Page 13: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/13.jpg)
例
100 == 10 * 10; // true‘abc’ == ‘def’; // falsetrue != false // true
true == 1 // true0 == ‘0’ // true‘’ != false // false
![Page 14: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/14.jpg)
同値演算子と非同値演算子
● 同値演算子「===」○ 左辺と右辺の値が同一であるかどうかを調べる
● 非同値演算子「!==」○ 左辺と右辺の値が同一でないかどうかを調べる
型が異なる時点で前者なら false、後者なら true
![Page 15: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/15.jpg)
例1
100 === 10 * 10; // true‘abc’ === ‘def’; // falsetrue !== false // true
true === 1 // false0 === ‘0’ // false‘’ !== false // true
![Page 16: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/16.jpg)
例2var a = { a: ‘hoge’, b: ‘piyo’};var b = { a: ‘hoge’, b: ‘piyo};a === b // falsea === a // true
オブジェクト型は参照で同値かどうかを判定される
![Page 17: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/17.jpg)
アジェンダ
1. 変数
2. 等値演算子と同値演算子
3. for/in 文
![Page 18: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/18.jpg)
for/in 文
● オブジェクトのプロパティを巡回する
● ただし、組み込みのプロパティやメソッドは調べられない○ toString()○ valueOf()
![Page 19: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/19.jpg)
例
var obj = { n: 3, s: ‘test’, b: true};for (var p in obj) { console.log(p); // n, s, b console.log(obj[p]); // 3, test, true}
![Page 20: JS 6th edition reading circle part 3](https://reader038.vdocuments.pub/reader038/viewer/2022100518/5597f1101a28ab9d758b46c7/html5/thumbnails/20.jpg)
お わ り