js 6th edition reading circle part 2

29
2型と値 (工事中) 開発部 てるー

Upload: teloo

Post on 20-Aug-2015

235 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: JS 6th edition reading circle part 2

第2回型と値 (工事中)

開発部てるー

Page 2: JS 6th edition reading circle part 2

アジェンダ

1. 基本型の値

2. 型変換

Page 3: JS 6th edition reading circle part 2

アジェンダ

1. 基本型の値

2. 型変換

Page 4: JS 6th edition reading circle part 2

● 基本型○ 数値○ 文字列○ 論理値○ null○ undefined

● オブジェクト型○ 上記以外

Page 5: JS 6th edition reading circle part 2

数値

● 整数と浮動小数点数を区別しない

● 64ビット浮動小数点形式

Page 6: JS 6th edition reading circle part 2

数値リテラル

30xff03773.146.02e23

Page 7: JS 6th edition reading circle part 2

文字列

● リテラルは「’」もしくは「”」を使う○ Google のコーディング規約は「’」を推奨○ 例

■ 「’」● Backbone● AngularJS

■ 「”」● jQuery

Page 8: JS 6th edition reading circle part 2

文字列リテラル

’testing’‘ほげほげ’ + ‘ぴよぴよ’‘This string¥nhas two lines’

‘<p><a href=”#jquery”>jQuery</p>’“<p><a href=¥”#jquery¥”>jQuery</p>”

Page 9: JS 6th edition reading circle part 2

論理値

● 予約語 true と false で表現

Page 10: JS 6th edition reading circle part 2

null と undefined

● null○ 「値がない」ことを示す○ プログラムレベルで予定どおりの場合を表すもの

● undefined○ 「値がない」ことを示す

○ システムレベルで予期せぬ、エラーのような場合に表す

もの

Page 11: JS 6th edition reading circle part 2

undefined (1 / 2)

var hoge = ‘test’;console.log(hoge); // test

var piyo;console.log(piyo); // undefined

Page 12: JS 6th edition reading circle part 2

undefined (2 / 2)

var executeA = function() { return 1 + 2; };console.log(executeA()); // 3

var executeB = function() { 1 + 2; };console.log(executeB()); // undefined

Page 13: JS 6th edition reading circle part 2

イメージ

‘test’ null

var hoge = ‘test’; var hoge = null; var hoge; // undefined

Page 14: JS 6th edition reading circle part 2

アジェンダ

1. 基本型の値

2. 型変換

Page 15: JS 6th edition reading circle part 2

hoge は文字列なはずなのに・・・?

var hoge = ‘256’;

console.log(hoge / 16); // 16if (hoge) { console.log(‘Hello!’); } // Hello!console.log(hoge.indexOf(‘56’)); // 1

Page 16: JS 6th edition reading circle part 2

状況に応じて柔軟に型が変換される

Page 17: JS 6th edition reading circle part 2

JavaScript第6版P49 表3-2からの引用

Page 18: JS 6th edition reading circle part 2

ラッパーオブジェクト (1 / 2)

var s = new String(‘ABC’);console.log(s.toLocaleLowerCase()); // abc

var n = new Number(12300000);console.log(n.toExponential()); // 1.23e+7

var b = new Boolean(false);if (b) { console.log(b); } // true

Page 19: JS 6th edition reading circle part 2

ラッパーオブジェクト (2 / 2)

console.log( ‘ABC’.toLocaleLowerCase()); // abc

console.log( 12300000.toExponential()); // 1.23e+7

Page 20: JS 6th edition reading circle part 2

明示的な型変換

Number(‘3’) // 3String(false); // ‘false’Boolean([]) // trueObject(3) // new Number(3)

Page 21: JS 6th edition reading circle part 2

オブジェクトから基本型への変換

すべてのオブジェクトは変換メソッドを 2 つもっている

● toString()● valueOf()

Page 22: JS 6th edition reading circle part 2

toString()

オブジェクトを表す文字列を返す

new Number(3).toString() // ‘3’({x: 1, y: 2}).toString() // ‘[object Object]’[1, 2, 3].toString // ‘1,2,3’

Page 23: JS 6th edition reading circle part 2

valueOf()

はっきり定義されていない

基本的は、オブジェクトを基本型値に変換するのが仕事

new Number(3).valueOf(); // 基本値型の 3new String(‘abc’).valueOf(); // 基本値型の ‘abc’({x: 1}).valueOf() // そのままオブジェクトを返す

Page 24: JS 6th edition reading circle part 2

オブジェクトから文字列に変換 (1 / 2)

1. toString() の実行を試みる

2. valueOf() の実行を試みる

3. TypeError を投げる

Page 25: JS 6th edition reading circle part 2

オブジェクトから文字列に変換 (2 / 2)var hoge = { toString: function() { return ‘toString’; }, valueOf: function() { return ‘valueOf’; }};

console.log(hoge); // toString

Page 26: JS 6th edition reading circle part 2

オブジェクトから数値に変換 (1 / 3)

1. valueOf() の実行を試みる

2. toString() の実行を試みる

3. TypeError を投げる

Page 27: JS 6th edition reading circle part 2

オブジェクトから文字列に変換 (2 / 3)var hoge = { toString: function() { return 1; }, valueOf: function() { return 2; }};

console.log(hoge - 2); // 0

Page 28: JS 6th edition reading circle part 2

オブジェクトから文字列に変換 (3 / 3)var hoge = { toString: function() { return 1; }, valueOf: function() { return { a: 2 }; }};

console.log(hoge - 2); // -1

Page 29: JS 6th edition reading circle part 2

お わ り