lpi-japan主催 · java エンジニアの ......
TRANSCRIPT
![Page 1: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/1.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
Javaエンジニアのための
HTML5プロフェッショナル認定試験 レベル2
解説無料セミナー
LPI-Japan主催
アシアル株式会社 生形可奈子
2016年1月23日
![Page 2: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/2.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/2
アシアルは、「エンジニアリングでインターネットの成長を牽引する」という
事業コンセプトのもと、HTML5・JavaScript等のWeb技術をベースとした
デベロッパー支援事業を行っています。
モバイルアプリ開発環境 UI/UX設計 システム構築・コンサル セミナー・スクール
![Page 3: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/3.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/3
生形 可奈子(うぶかた・かなこ)
• 講師・Monacaエバンジェリスト
• Java/C#を中心とした業務・制御系エンジニア出身
• 著書:「スラスラわかるJavaScript」
「Monacaで学ぶはじめてのプログラミング
~モバイルアプリ入門編~」
![Page 4: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/4.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
HTML5プロフェッショナル認定試験とは
• 概要
• 試験範囲
頻出・難解ポイント解説
• JavaScriptのデータ型
• オブジェクト
• 文字列/配列
• 関数とスコープ
• prototype/継承
4
![Page 5: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/5.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/5
HTML5プロフェッショナル認定試験
![Page 6: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/6.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
• 2014年10月正式勧告
• リッチクライアント・アプリケーションのプラットフォーム
• マルチデバイス対応・マルチメディア対応
• 広義ではCSS3やJavaScriptによる3Dグラフィック、WebSocket、デバイスアクセス、クライアントストレージ等も含む
6
![Page 7: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/7.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
特定非営利活動法人LPI-Japanが実施する、HTML5および周辺技術の知識レ
ベルを測る認定制度です。
試験の難易度を示す2種類のレベルがあり、段階的に受験します。
• Level1
マルチデバイスに対応した静的なWebコンテンツを HTML5を使ってデザイン、作
成できるレベル
• Level2
システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的
Webコンテンツの開発・設計ができるレベル
7
![Page 8: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/8.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
出題範囲 重要度
JavaScript
JavaScript文法 ★★★★★★★★★★ 10
WebブラウザにおけるJavaScript API
イベント ★★★★★★★★ 8
ドキュメントオブジェクト/DOM ★★★★★★ 6
ウィンドウオブジェクト ★★★★★★★★ 8
グラフィックス
Canvas(2D) ★★★★★★ 6
8
![Page 9: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/9.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
試験方式はコンピュータベーストテスト(CBT)です。試験配信会
社の「ピアソンVUE」を通して受験します。
問題数 40〜45問
試験時間 90分
合格ライン 約7割
回答方式 殆どが選択式(複数回答あり)記述式も1問程度コードリーディング問題が多い
受験料 ¥15,000(税抜)
9
![Page 10: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/10.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
JavaScriptについて
10
![Page 11: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/11.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
JavaScriptのはじまり
• 当初はMocha、LiveScriptという名前だった
• 開発元のNetscape社とSun社(現Oracle)の提携により、JavaScript
に改名される
• 初期はWebページに簡単な動きを付けたり、フォーム入力を補助するこ
とが目的の簡易的言語だった
11
![Page 12: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/12.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
JavaScript最新動向
• Gmailなどのクラウドサービスの登場により、ブラウザ上で実現できる
ことが増え、JavaScriptが脚光を浴びるようになる
• 様々なJavaScript製フレームワークが台頭してきたことにより、大規模
なWebアプリケーションの構築が可能となった
• 進化系として、サーバサイドプログラムの構築やスマートフォンアプリ
の制作にも用いられるケースが増えてきた
12
![Page 13: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/13.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
JavaScriptの標準仕様
• JavaScriptは、各ブラウザに独自に実装されており、それぞれ 独自仕様
が含まれている場合がある
• ActionScriptやJScript等、JavaScript以外の言語も存在する
• 上記言語を標準化するために作られた仕様が「EcmaScript」
• 言語仕様のみを定義しており、HTML5 APIやDOMは含まれない
最新バージョンは6
• 2015年6月17日に承認
16
![Page 14: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/14.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
JavaScriptが影響を受けている言語
• コードの記述方法はJavaに似ている
• 言語仕様として最も影響を受けているのは、SchemeとSelf
JavaとJavaScriptの主な違い
14
言語 型システム オブジェクト指向分類
Java 静的型付け クラスベース
JavaScript 動的型付け プロトタイプベース
![Page 15: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/15.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
JavaScriptのデータ型
15
![Page 16: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/16.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
JavaScriptは動的型付け言語
• データの型は実行時に決定されるため、変数宣言時に型を限定しない
変数宣言の例
実行時にデータ型が判定される
16
var 変数名;
console.log(1 + 1); // 2console.log(1 + '1'); // 11
![Page 17: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/17.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
データ型一覧
17
型の分類 代表的な型
プリミティブ型(1つの値のみを持つ)
数値、文字列、論理値
null(値が存在しない)
undefined(値が定義されていない)
オブジェクト型(プロパティの集合体)
オブジェクト、関数、配列など
![Page 18: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/18.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/18
null … 値が存在しないことを意図的に示す値
• getElementById()で取得しようとする要素がHTML文書内に存在しな
い場合などに、メソッドの戻り値として返却される
undefined … 値がそもそも定義されていないことを示す値
• 初期化されていない変数、定義されていないプロパティ、return文の
ない関数の戻り値などはすべてundefinedで表される
![Page 19: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/19.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/19
JavaScriptの等価演算子には二種類ある
• 値が等しいかどうかのみを比較する緩やかな等価演算子
• データの型まで含めて等しいかどうかを比較する厳密な等価演算子
演算子 説明 例 結果
== 緩やかな等価演算子 5 == '5' true
=== 厳密な等価演算子 5 === '5' false
!= 緩やかな不等価演算子 5 != '5' false
!== 厳密な不等価演算子 5 !== '5' true
![Page 20: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/20.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
条件式における値の評価
• 論理値以外の値をif文などの条件式として指定すると、以下のように
判定される
20
true false
0以外の数値
1文字以上の文字列
オブジェクト(中身が空でも)
0
空文字("")
undefined
null
![Page 21: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/21.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/21
引数の省略チェックなどに用いられる
function check(x) {// 引数xが空だったらエラーにするif(!x) {
console.log("エラー:値が入っていません");return;
}// 以降、省略
}
![Page 22: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/22.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/22
0や空文字もfalseとして判定されることに注意
function check(x) {// 引数xが空だったらエラーにするif(!x) {
console.log("エラー:値が入っていません");return;
}// 以降、省略
}
check(); // 想定通りのエラーcheck(0); // 想定外のエラー
![Page 23: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/23.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
オブジェクトの基本
23
![Page 24: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/24.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/24
プロパティとメソッド
• オブジェクトは、複数のデータの集合体である
• オブジェクト内のデータには、オブジェクトの属性値を保持する「プロ
パティ」と、何らかの操作を行う「メソッド」がある
オブジェクト
プロパティ メソッド
![Page 25: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/25.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/25
Objectオブジェクトはすべてのオブジェクトの親となっている
Object
String(文字列) Array
(配列)
Function(関数)
・toString()
・valueOf()
・hasOwnProperty()…継承
![Page 26: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/26.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/26
オブジェクトの生成方法
• Objectオブジェクトの生成は、オブジェクトリテラル{}で代用することが可能
• Arrayブジェクトの生成は、配列リテラル[]で代用することが可能
オブジェクト生成の例
var 変数名 = new オブジェクトの型名(引数);
var list = new Array(1,2,3);
![Page 27: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/27.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/27
オブジェクト生成時の流れ
① オブジェクトの生成時に、コンストラクタという関数が実行される
② コンストラクタは、新しくオブジェクトを生成し、それを戻り値として
返却する(このオブジェクトをインスタンスと呼ぶ)
var list = new Array(1,2,3);
list Arrayのコンストラクタ
![Page 28: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/28.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/28
typeof演算子
• データの型を調べる演算子なので、StringやArrayなどのオブジェクト
はほぼすべて「object」と判定される
※ 関数は「function」と判定される
var list = new Array(1,2,3);console.log(typeof list); // object
![Page 29: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/29.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/29
instanceof演算子
• どのコンストラクタによって生成されたインスタンスなのかを判定する
ので、オブジェクトの種類を調べることができる
var list = new Array(1,2,3);console.log(list instanceof Array); // true
![Page 30: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/30.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
ユーザー定義オブジェクト
30
![Page 31: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/31.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/31
ユーザー定義オブジェクトの作成
• newを使用
• オブジェクトリテラルを使用
var オブジェクト名 = new Object();
var オブジェクト名 = {};
![Page 32: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/32.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/32
プロパティへのアクセス
• ドットを使用
• 角かっこを使用
変数名.プロパティ名
変数名["プロパティ名"]
![Page 33: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/33.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/33
オブジェクトの生成とプロパティへのアクセス
var obj = { name: "生形"
};
// プロパティへのアクセス
console.log(obj.name);
// 以下のように記述することも可能
var propertyName = "name"
console.log(obj[propertyName]);
![Page 34: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/34.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
文字列
34
![Page 35: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/35.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/35
文字列リテラルは、以下の二種類
• シングルクォート・ダブルクォートのどちらでも可
文字列にはプリミティブ型とオブジェクト型の二種類がある
• 文字列リテラルはプリミティブ型となる
'こんにちは'"こんにちは"
var str1 = "こんにちは"; // プリミティブ型var str2 = new String("こんにちは"); // オブジェクト型
![Page 36: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/36.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/36
プリミティブ型(文字列)
"hoge"
オブジェクト型(String)
・length・replace()・substr()"hoge"
…
プリミティブ型は1つの値だけ、オブジェクト型は複数のメソッド
やプロパティを持つ
![Page 37: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/37.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
ラッパーオブジェクト … プリミティブ型に対応するオブジェクト
37
プリミティブ型 ラッパーオブジェクト
数値 Number
文字列 String
論理値 Boolean
![Page 38: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/38.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
以下の実行結果は?
問題:プリミティブ型に対してプロパティを指定
38
console.log("hoge".length);
A) 4
B) null
C) undefined
D) エラー
![Page 39: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/39.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
プロパティを呼び出した場合、プリミティブ値から対応するラッ
パーオブジェクトへの暗黙的な変換が行われる
39
"hoge".length
"hoge"
・length・replace()・substr()"hoge"
…
プリミティブ型
ラッパーオブジェクト
変換
![Page 40: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/40.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
以下の実行結果は?(true / false)
問題:プリミティブ型とラッパーオブジェクトの比較
40
var a = "hoge";var b = new String("hoge");console.log(a == b);
以下の実行結果は?(true / false)
var a = "hoge";var b = new String("hoge");console.log(a === b);
![Page 41: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/41.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
以下の実行結果は?(true / false)
問題:値の比較、参照の比較
41
var a = "hoge";var b = "hoge";console.log(a == b);
以下の実行結果は?(true / false)
var a = new String("hoge");var b = new String("hoge");console.log(a == b);
![Page 42: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/42.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
プリミティブ型は値そのものを比較する
42
オブジェクト型は参照しているオブジェクトが同じものかどうかを
比較する
a b
"hoge" "hoge"
![Page 43: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/43.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/43
a b
"hoge""hoge"
var a = new String("hoge");var b = a;console.log(a == b);
この場合は同じオブジェクトを参照しているのでtrue
![Page 44: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/44.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
配列
44
![Page 45: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/45.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/45
配列の作成
• newを使用
• 配列リテラルを使用
var 配列名 = new Array(1, 2, 3);
var 配列名 = [1, 2, 3];
![Page 46: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/46.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/46
var list = []; // 空の配列を作成
list[0] = "AAA";
list[1] = "BBB";
list[2] = "CCC";
配列の要素数は柔軟に変更することが可能
![Page 47: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/47.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
以下の実行結果は?
47
var array = [10, 20, 30];array[9] = 99;console.log(array.length);
A) 3
B) 4
C) 10
D) undefined
![Page 48: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/48.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/48
0 1 2
10 20 30
9
99
インデックスは連番でなくても割り振ることができる
lengthプロパティは最大インデックス+1を返す
![Page 49: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/49.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
関数
49
![Page 50: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/50.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
関数もオブジェクトの一種
• 関数はFunctionという名前のオブジェクトである
• オブジェクトの一種なので、変数の値として代入することが可能。
オブジェクトのメソッドは、Fuctionオブジェクトをプロパティに代入
することで実現している
50
var obj = {method1: function (message) {
alert(message);}
};
![Page 51: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/51.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
関数宣言
51
func("hello");function func(message) {
alert(message);}
• 関数にfuncという名前をつけている
• 関数は静的に作成される(スクリプト実行前に評価される)
![Page 52: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/52.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
関数式
52
var func = function(message) {alert(message);
};func("hello");
• funcという変数に関数を代入している
• この場合、関数名は省略できる(無名関数や匿名関数と呼ばれる)
![Page 53: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/53.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
即時関数
53
(function(message) {alert(message);
})("hello");
• (関数の定義)(引数); とすることで定義と呼び出しを同時に行う
• 主にスコープを限定する用途などで利用される
![Page 54: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/54.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
デフォルト引数
可変長引数
アロー記法
54
![Page 55: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/55.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
デフォルト引数
55
function f(x, y=20){console.log(x); // 10console.log(y); // 20
}
f(10);
• 引数に値が渡されなかった場合に代入する値を指定することができる
![Page 56: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/56.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
可変長引数
56
function f(...args) {for(var i=0; i<args.length; i++) {
console.log(args[i]);}
}
f(10, 20);f(10, 20, 30, 40);
• 引数の数を動的に変更することができる
![Page 57: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/57.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
従来の記法
57
var f = function(x) { return x * 2; };
• functionキーワードを省略できる
• 引数が1つの場合は、()を省略できる
• 処理がreturn文のみの場合は、{}とreturnキーワードを省略できる
アロー記法
var f = x => x * 2;
![Page 58: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/58.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
変数のスコープ
58
![Page 59: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/59.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
変数はスコープ(有効範囲)内でのみ参照できる
59
var a = 10; // aはグローバル変数なのでどこからでも参照可
function func1() {
var b = 20;
function func2() {
var c = 30;
}
}
• スコープは関数単位で作られる
cのスコープbのスコープ
![Page 60: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/60.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
スコープ外で変数を参照した場合、ReferenceErrorが発生する
60
var a = 10;function func1() {
var b = 20;function func2() {
var c = 30;}
}
func1();console.log(a); // 10console.log(b); // ReferenceError
![Page 61: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/61.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
varをつけていない場合、その変数はグローバルスコープとなる
61
var a = 10;function func1() {
b = 20;
function func2() {var c = 30;
}}
func1();console.log(a); // 10console.log(b); // 20
varを付け忘れているので、aと同じグローバル変数になる
![Page 62: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/62.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
use strict(厳格モード)を宣言すると、varの付け忘れなどの、
推奨されない記述に対して警告が表示される
62
function func() {
"use strict";
b = 20; // エラー
}
![Page 63: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/63.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/63
スクリプト内の値やオブジェクトは全て、暗黙的に一番外側に存
在しているグローバルオブジェクトに含まれている
グローバルオブジェクト<script>var a = 10;var obj = {
prop: "hoge"};
</script>
a
obj
prop
![Page 64: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/64.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/64
ブラウザ環境におけるグローバルオブジェクトは、「Windowオ
ブジェクト」である
Windowオブジェクト
alert() document
open()
setInterval()
location
history
![Page 65: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/65.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/65
グローバル変数=グローバルオブジェクトのプロパティ
<script>var a = 10;
console.log(a); // 10console.log(window.a); // 10</script>
![Page 66: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/66.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/66
複数のJSファイルを読み込んでいる場合に、グローバル変数が競
合してしまう場合があるので利用は必要最小限に
lib1.js…<script src="lib1.js"><script src="lib2.js">…
index.html
lib2.js
var x = 10;
var x = 20;
xが競合
![Page 67: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/67.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
シャドーイング(スコープの重複)は許可されている
67
var x = 10; // グローバルスコープ
function func1() {
var x = 20; // ローカルスコープ
console.log(x); // 20
}
• 上記の場合、グローバルスコープとローカルスコープに同名の変数が宣言
されている。こういった場合はローカルスコープの変数が優先される
![Page 68: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/68.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
以下の実行結果は?
68
for(var i = 0; i < 3; i++) {var a = "hoge";
}console.log(a);
A) エラー
B) undefined
C) null
D) "hoge"
![Page 69: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/69.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
JavaScriptにはブロックスコープがない
• スコープが発生するのは関数内のみ
• ただし、ES6ではブロックスコープを有効にするletキーワードが追加
されている
69
if(true) {let a = "hoge";
}
console.log(a); // a is not defined
![Page 70: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/70.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
以下の実行結果は?
70
var a = "global";function func() {
console.log(a);var a = "func";
}func();
A) エラー
B) undefined
C) "global"
D) "func"
![Page 71: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/71.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
ホイスティング(変数の巻き上げ)とは
71
var a = "global";function func() {
var a;console.log(a);a = "func";
}func();
• 関数の途中で宣言された変数は、関数の先頭に巻き上げて宣言される
• ただし、初期値として代入されている値は巻き上げない
• 先ほどのコードは以下のように解釈される
変数宣言は、必ずスコープの先頭で行うこと
![Page 72: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/72.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
prototype
72
![Page 73: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/73.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/73
JavaScriptはプロトタイプベースのオブジェクト指向言語
• あるオブジェクトを基礎として、新しいオブジェクトを生成する仕組み
• 新しく作られたオブジェクトは、「インスタンス」と呼ばれる
クラスベース プロトタイプベース
クラス プロトタイプオブジェクト
オブジェクト
(インスタンス)
オブジェクト
(インスタンス)
オブジェクト
(インスタンス)
オブジェクト
(インスタンス)
![Page 74: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/74.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/74
組み込みオブジェクトもプロトタイプベースで作られている
[0] [1] [2]
Array
10 20 30
[0] [1] [2]
'A' 'B' 'C'
インスタンス
プロトタイプオブジェクト
var list1 = new Array(10, 20, 30);
var list2 = new Array('A', 'B', 'C');
![Page 75: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/75.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/75
プロトタイプオブジェクト=関数
• 基礎となるオブジェクト(クラスベース言語におけるクラスの役割)は、
関数オブジェクトである
• プロトタイプオブジェクトを元にインスタンスを生成するには、new
演算子を使う
var Dog = function() { };
var dog1 = new Dog();
![Page 76: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/76.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/76
コンストラクタ=関数の処理
• 関数内の処理はインスタンスを生成(new)した時に、コンストラクタ
として実行される
var Dog = function(_name) {this.name = _name;this.show = function() {
alert(this.name);};
};
var dog1 = new Dog("ポチ"); // コンストラクタを実行
this = インスタンス
![Page 77: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/77.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/77
インスタンス=Objectオブジェクト
• プロトタイプオブジェクトはFunction型だが、インスタンスは
Object型となる
var Dog = function(_name) {// 省略
};var dog1 = new Dog("ポチ");
console.log(typeof Dog); // functionconsole.log(typeof dog1); // object
![Page 78: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/78.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/78
コンストラクタの暗黙的処理
• インスタンスがObject型になるのは、コンストラクタが暗黙的に新しい
Objectを生成しているためである。
var Dog = function(_name) {var this = {};this.name = _name;this.show = function() {alert(this.name);
};return this;
};
var Dog = function(_name) {this.name = _name;this.show = function() {
alert(this.name);};
};
ソースコード 暗黙的処理
![Page 79: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/79.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/79
オブジェクトのコア
• プロトタイプオブジェクトが持つプロパティ
• 各インスタンスは、プロトタイプオブジェクトのprototypeを参照する
• prototypeプロパティには、各インスタンスで共通の機能を格納する
オブジェクト
(インスタンス)
オブジェクト
(インスタンス)
プロトタイプオブジェクト
prototypeプロパティ
![Page 80: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/80.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/80
オブジェクトをインスタンス化すると、元のオブジェクトで定義されたプロ
パティとメソッドが各インスタンスにコピーされる
var Dog = function(_name) {
this.name = _name;
this.show = function() {
alert(this.name);
};
};
var dog1 = new Dog("ポチ");
var dog2 = new Dog("シロ");
name
show()
name: ポチ
show()
name: シロ
show()
Dog
dog2
dog1
コピー
![Page 81: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/81.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/81
各インスタンスが内部的に持つ__proto__プロパティから元のオブ
ジェクトのprototypeプロパティを参照する
var Dog = function(_name) {
this.name = _name;
};
Dog.prototype.show =
function() {
alert(this.name);
};
var dog1 = new Dog("ポチ");
var dog2 = new Dog("シロ");
name
name: ポチ
__proto__
name: シロ
__proto__
Dog
dog2
dog1
参照
show()
prototype
![Page 82: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/82.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/82
以下は、Arrayのインスタンスからprototypeに定義されているjoinメソッ
ドを呼び出している例
var list = ['a', 'b', 'c'];
var str = list.join('+');
console.log(str); //a+b+c
[0]: a
[1]: b
[2]: c
__proto__
Arraylist
参照
joinメソッド
popメソッド
sortメソッド
…
prototype
![Page 83: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/83.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
継承
83
![Page 84: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/84.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/84
子オブジェクト
・機能C
継承とは
• あるオブジェクトを拡張して、別のオブジェクトを作る仕組み
親オブジェクト
・機能A・機能B
親オブジェクト
・機能A・機能B
継承
![Page 85: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/85.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/85
最も単純な継承関係
• 子のprototypeプロパティに、親のインスタンスを代入する
// 親var Parent = function() {
this.x = 10;};Parent.prototype.show = function() { alert(this.x); };
// 子var Child = function() {
this.y = 20;};
// 継承Child.prototype = new Parent();
var child = new Child();child.show();
![Page 86: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/86.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/86
プロパティを探索する順序
①自身のインスタンス内に該当プロパティがあるかどうかを調べる
②プロトタイプオブジェクトのprototype内を調べる
③親オブジェクトのprototype内を調べる
![Page 87: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/87.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/87
親オブジェクト 子オブジェクト
![Page 88: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/88.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/88
子のprototypeプロパティに、親が持つメソッドと同名のメソッドを定義する
// 親var Parent = function() {
this.x = 10;};Parent.prototype.show = function() { alert(this.x); };
// 子var Child = function() {
this.y = 20;};
// 継承Child.prototype = new Parent();
// メソッドのオーバーライドChild.prototype.show = function() {alert(this.x + this.y);
};
var child = new Child();child.show();
![Page 89: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/89.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/89
xプロパティ yプロパティ
__proto__
Parent Childインスタンス
参照
yプロパティ
Child
prototype
new
①
参照showメソッド
new
prototype
③ xプロパティ
showメソッド
__proto__
Parentインスタンス
➁➁でshowメソッドがみつかったので、
③まで見ない
オーバーライドしたshowメソッドが
先にみつかる
![Page 90: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/90.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/90
for/in文
• すべてのユーザ定義プロパティを走査する
• プロトタイプチェーンを遡って走査される
var child = new Child();
for(var key in child){console.log(key + ':' + child[key]);
}
![Page 91: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/91.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/91
hasOwnProperty
• 自身が持つプロパティかどうかを調べ、論理値を返す
var child = new Child();
for(var key in child){
if(child.hasOwnProperty(key)) {
console.log(key + ':' + child[key]);
}
}
![Page 92: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/92.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/92
class構文により、クラスベース言語のように扱うことができる
class Parent {show(text) {console.log(text);
}};
class Child extends Parent {show() {super.show("child");
}};
キーワード1つで継承関係を実現
メソッドはprototype内に定義される
親オブジェクトの参照も容易
![Page 93: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/93.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
受験対策
93
![Page 94: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/94.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
HTML5 Web標準API バイブル
コア・JavaScript HTML5 API
94
レベル2対策本
HTML5プロフェッショナル認定試験レベル2攻略テキスト JavaScript 開眼!JavaScript
![Page 95: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/95.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/95
https://babeljs.io/
![Page 96: LPI-Japan主催 · Java エンジニアの ... システム間連携や最新のマルチメディア術に対応したWebアプリケーションや動的 Web ... • 2015 年6月17日に](https://reader035.vdocuments.pub/reader035/viewer/2022070906/5f76a5b8a841c92bb83823ea/html5/thumbnails/96.jpg)
© Asial Corporation 2014. All rights reserved.
The HTML5 Logo is licensed under Creative Commons Attribution 3.0. Unported by the W3C; http://creativecommons.org/licenses/by/3.0/
Open the Future with HTML5.
96