SlideShare a Scribd company logo
1 of 29
Download to read offline
第2回
型と値 (工事中)
開発部
てるー
アジェンダ
1. 基本型の値
2. 型変換
アジェンダ
1. 基本型の値
2. 型変換
型
● 基本型
○
○
○
○
○

数値
文字列
論理値
null
undefined

● オブジェクト型
○ 上記以外
数値
● 整数と浮動小数点数を区別しない
● 64ビット浮動小数点形式
数値リテラル
3
0xff
0377
3.14
6.02e23
文字列
● リテラルは「’」もしくは「”」を使う
○ Google のコーディング規約は「’」を推奨
○ 例
■ 「’」
●
●

Backbone
AngularJS

■ 「”」
●

jQuery
文字列リテラル
’testing’
‘ほげほげ’ + ‘ぴよぴよ’
‘This string¥nhas two lines’
‘<p><a href=”#jquery”>jQuery</p>’
“<p><a href=¥”#jquery¥”>jQuery</p>”
論理値
● 予約語 true と false で表現
null と undefined
● null
○ 「値がない」ことを示す
○ プログラムレベルで予定どおりの場合を表すもの

● undefined
○ 「値がない」ことを示す
○ システムレベルで予期せぬ、エラーのような場合に表す
もの
undefined (1 / 2)
var hoge = ‘test’;
console.log(hoge); // test
var piyo;
console.log(piyo); // undefined
undefined (2 / 2)
var executeA = function() { return 1 + 2; };
console.log(executeA()); // 3
var executeB = function() { 1 + 2; };
console.log(executeB()); // undefined
イメージ
var hoge = ‘test’;

‘test’

var hoge = null;

null

var hoge; // undefined
アジェンダ
1. 基本型の値
2. 型変換
hoge は文字列なはずなのに・・・?
var hoge = ‘256’;
console.log(hoge / 16); // 16
if (hoge) { console.log(‘Hello!’); } // Hello!
console.log(hoge.indexOf(‘56’)); // 1
状況に応じて柔軟に
型が変換される
JavaScript
第6版
P49 表3-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
ラッパーオブジェクト (2 / 2)
console.log(
‘ABC’.toLocaleLowerCase()); // abc
console.log(
12300000.toExponential()); // 1.23e+7
明示的な型変換
Number(‘3’) // 3
String(false); // ‘false’
Boolean([]) // true
Object(3) // new Number(3)
オブジェクトから基本型への変換
すべてのオブジェクトは変換メソッドを 2 つもってい
る
● toString()
● valueOf()
toString()
オブジェクトを表す文字列を返す

new Number(3).toString() // ‘3’
({x: 1, y: 2}).toString() // ‘[object Object]’
[1, 2, 3].toString // ‘1,2,3’
valueOf()
はっきり定義されていない
基本的は、オブジェクトを基本型値に変換するの
が仕事
new Number(3).valueOf(); // 基本値型の 3
new String(‘abc’).valueOf(); // 基本値型の ‘abc’
({x: 1}).valueOf() // そのままオブジェクトを返す
オブジェクトから文字列に変換 (1 / 2)
1. toString() の実行を試みる
2. valueOf() の実行を試みる
3. TypeError を投げる
オブジェクトから文字列に変換 (2 / 2)
var hoge = {
toString: function() {
return ‘toString’;
},
valueOf: function() {
return ‘valueOf’;
}
};
console.log(hoge); // toString
オブジェクトから数値に変換 (1 / 3)
1. valueOf() の実行を試みる
2. toString() の実行を試みる
3. TypeError を投げる
オブジェクトから文字列に変換 (2 / 3)
var hoge = {
toString: function() {
return 1;
},
valueOf: function() {
return 2;
}
};
console.log(hoge - 2); // 0
オブジェクトから文字列に変換 (3 / 3)
var hoge = {
toString: function() {
return 1;
},
valueOf: function() {
return { a: 2 };
}
};
console.log(hoge - 2); // -1
お わ り

More Related Content

More from Teloo

図とコード例で多分わかる React と flux (工事中)
図とコード例で多分わかる React と flux (工事中)図とコード例で多分わかる React と flux (工事中)
図とコード例で多分わかる React と flux (工事中)Teloo
 
Gitを理解するためにおさえておきたい3つの図(工事中)
Gitを理解するためにおさえておきたい3つの図(工事中)Gitを理解するためにおさえておきたい3つの図(工事中)
Gitを理解するためにおさえておきたい3つの図(工事中)Teloo
 
図でわかるGit
図でわかるGit図でわかるGit
図でわかるGitTeloo
 
JS 6th edition reading circle part 3
JS 6th edition reading circle part 3JS 6th edition reading circle part 3
JS 6th edition reading circle part 3Teloo
 
(工事中) Git の仕組み
(工事中) Git の仕組み(工事中) Git の仕組み
(工事中) Git の仕組みTeloo
 
(工事中) Git の仕組み
(工事中) Git の仕組み(工事中) Git の仕組み
(工事中) Git の仕組みTeloo
 

More from Teloo (6)

図とコード例で多分わかる React と flux (工事中)
図とコード例で多分わかる React と flux (工事中)図とコード例で多分わかる React と flux (工事中)
図とコード例で多分わかる React と flux (工事中)
 
Gitを理解するためにおさえておきたい3つの図(工事中)
Gitを理解するためにおさえておきたい3つの図(工事中)Gitを理解するためにおさえておきたい3つの図(工事中)
Gitを理解するためにおさえておきたい3つの図(工事中)
 
図でわかるGit
図でわかるGit図でわかるGit
図でわかるGit
 
JS 6th edition reading circle part 3
JS 6th edition reading circle part 3JS 6th edition reading circle part 3
JS 6th edition reading circle part 3
 
(工事中) Git の仕組み
(工事中) Git の仕組み(工事中) Git の仕組み
(工事中) Git の仕組み
 
(工事中) Git の仕組み
(工事中) Git の仕組み(工事中) Git の仕組み
(工事中) Git の仕組み
 

Recently uploaded

論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...Toru Tamaki
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A surveyToru Tamaki
 
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案sugiuralab
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNetToru Tamaki
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)Hiroki Ichikura
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものですiPride Co., Ltd.
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Yuma Ohgami
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdftaisei2219
 

Recently uploaded (8)

論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
 
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
TataPixel: 畳の異方性を利用した切り替え可能なディスプレイの提案
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
 
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
【早稲田AI研究会 講義資料】3DスキャンとTextTo3Dのツールを知ろう!(Vol.1)
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
 

JS 6th edition reading circle part 2