js 6th edition reading circle part 3

20

Click here to load reader

Upload: teloo

Post on 04-Jul-2015

116 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: JS 6th edition reading circle part 3

第3回変数・演算子・文 (工事中)

開発部てるー

Page 2: JS 6th edition reading circle part 3

アジェンダ

1. 変数

2. 等値演算子と同値演算子

3. for/in 文

Page 3: JS 6th edition reading circle part 3

アジェンダ

1. 変数

2. 等値演算子と同値演算子

3. for/in 文

Page 4: JS 6th edition reading circle part 3

変数の宣言

var x;var i, sum;

var message = ‘hello’;var a = 0, b = 0, c = 0;

Page 5: JS 6th edition reading circle part 3

グローバル変数とローカル変数

● グローバル変数○ スコープ(※)はプログラム全体

● ローカル変数○ スコープ(※)は変数が宣言された関数の中に限定

※ スコープ: 変数の有効範囲

Page 6: JS 6th edition reading circle part 3

グローバル変数とローカル変数

var g1 = ‘global’; // グローバル変数

function checkscope() { var local = ‘local’; // ローカル変数

g2 = ‘global’; // グローバル変数

};checkscope();

Page 7: JS 6th edition reading circle part 3

同じ名前があった場合はローカルを優先

var scope = ‘global’;function checkscope() { var scope = ‘local’; return scope;};checkscope(); // local

Page 8: JS 6th edition reading circle part 3

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

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

問題

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

アジェンダ

1. 変数

2. 等値演算子と同値演算子

3. for/in 文

Page 12: JS 6th edition reading circle part 3

等値演算子と不等演算子

● 等値演算子「==」○ 左辺と右辺の値が等しいかどうかを調べる

● 不等演算子「!=」○ 左辺と右辺の値が等しくないかどうかを調べる

両方とも型変換を行いながら比較する

Page 13: JS 6th edition reading circle part 3

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

同値演算子と非同値演算子

● 同値演算子「===」○ 左辺と右辺の値が同一であるかどうかを調べる

● 非同値演算子「!==」○ 左辺と右辺の値が同一でないかどうかを調べる

型が異なる時点で前者なら false、後者なら true

Page 15: JS 6th edition reading circle part 3

例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

例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

アジェンダ

1. 変数

2. 等値演算子と同値演算子

3. for/in 文

Page 18: JS 6th edition reading circle part 3

for/in 文

● オブジェクトのプロパティを巡回する

● ただし、組み込みのプロパティやメソッドは調べられない○ toString()○ valueOf()

Page 19: JS 6th edition reading circle part 3

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

お わ り