SlideShare ist ein Scribd-Unternehmen logo
1 von 27
Title:
“Chotto Dive into
Object of JavaScript”,
Author: “@youta”
}
{
{
}
name: “小椋 陽太”,
twitter: “@youtsua_o”,
description: “”,
JAPANGLIS
H
Do you know
?
{
}
目次
1: “JavaScriptは全てがObject?”
2: “Objectの扱い方”
3: “prototypeとは”
4: “おまけ”
{
}
”JavaScriptは全てがObject?”
JavaScriptって全部Objectなんだよね?
{
}
”JavaScriptは全てがObject?”
いいえ、JavaScriptにはちゃんとプリミティブが存在します。
“youta” true 123
null undefined
{
}
”JavaScriptは全てがObject?”
プリミティブじゃないと勘違いされる理由。
“youta”.toLocaleUpperCase()
=> “YOUTA”
true.toString()
=> “true”
{
}
”JavaScriptは全てがObject?”
なぜ?
JavaScriptが一時的にプリミティブ値を
対応するラッパーオブジェクトに変換しているから。
{
}
”JavaScriptは全てがObject?”
ビルトインオブジェクト達(一部)
Object Function Boolean String
Number Math Date Error
{
}
“Objectの扱い方”
ビルトインオブジェクトも純粋なただのオブジェクトです。
こんな風に文字列型や数値型を定義することも可能です。
var str = new String(“youta”)
var num = new Number(123)
※ typeof str // => object となります。
{
}
これらのObjectはnew演算子を使って呼び出せる
コンストラクタを持っています。
ビルトインと同じように自分で作成することもできます。
“Objectの扱い方”
var Me = function (name, age) {
this.name = name
this.age = age
}
var me = new Me(“youta”, 24)
{
}
“Objectの扱い方”
Functionも例外ではありません。
() という演算子が特殊な役割を与えているだけです。
var square = new Function(
“width”, “height”, // 引数
“return width * height” // 処理
)
square(10, 20) // => 200
{
}
でもMathオブジェクトは例外です。
コンストラクタを持っておらず、new 演算子は使えません。
var math = new Math() // => Error!
“Objectの扱い方”
Math.random() // => OK
{
}
Mathオブジェクトのようにコンストラクタを持たない
オブジェクトの作り方の方が馴染み深いですね。
var fizzBuzz = {
fizz: function (n) { return n % 3 ? n : “fizz” },
buzz: function (n) { return n % 5 ? n : “buzz” },
...
}
var fb = new fizzBuzz() // => Error!
“Objectの扱い方”
{
}
“prototypeとは”
ところで、話を戻して、
これは当然のように見えますが、
なぜ定義していない関数が使えるのでしょうか?
var str = ”youta”
str.toLocaleUpperCase()
{
}
Stringオブジェクトに組み込まれているからです!
じゃあ、
これはどうして使えるのでしょうか?
“prototypeとは”
var str = ”youta”
str.hasOwnProperty(“hoge”)
{
}
JavaScriptのプロトタイプチェーンのおかげです!!
全てのオブジェクトの根源はObjectです。
全てのオブジェクトはObjectのプロパティを受け継ぎ、
使用することができます。
この受け継いだプロパティをプロトタイプといい、
プロトタイプを遡ってプロパティを利用する一連の流れを
プロトタイプチェーンといいます!!
“prototypeとは”
{
}
“prototypeとは”
プロトタイプチェーンの図
Object.prototype.hasOwnProperty
// => あった!
var str = new String(“youta”)
str.hasOwnProperty(“hoge”)
String.prototype.hasOwnProperty
// => ない
str.hasOwnProperty
// => ない
{
}
“prototypeとは”
(ここでプロトタイププロパティもプロトタイプを持っていて、
全てObjectのプロトタイプが終着点になる話を熱く語る)
{
}
“おまけ”
ES2015ではついにJavaScriptにもclassの概念が生まれました!
そしてそのclassもObjectの特性を使っているよという話。
{
}
”classの実装”
classはどのようにトランスパイルされているのでしょうか?
先ほど自分で作成したコンストラクタと同じですね!
class Hoge {
hoge = 'hoge'
}
var Hoge = /** @class */
(function () {
function Hoge() {
this.hoge = 'hoge';
}
return Hoge;
}());
{
}
”classの実装”
extendはどうでしょうか?
class Fuga extends Hoge {
fuga: string = 'fuga'
}
{
}
”classの実装”
extendはどうでしょうか?
{
}
”classの実装”
super class のプロパティが
プロトタイプとして再定義されていますね!
JavaScriptの標準機能であるプロトタイプチェーンを
活用して、他言語のclassと同等の機能を実現しています!
{
}
(アクセス修飾子は存在しないので、
平気でプロトタイププロパティでもなんでも
書き換えられてしまいます。
private とか使いたい場合は
素直にTypeScriptを使いましょう。)
”classの実装”
{
}
”classの実装”
ちなみに static はTypeScriptの力を借りなくても実現可能です!
class Fuga {
static fuga = ‘fuga'
}
var Fuga = /** @class */
(function () {
function Fuga() {
}
Fuga.fuga = 'fuga';
return Fuga;
}());
{
}
Arigathanks
gozaimuch!

Weitere ähnliche Inhalte

Empfohlen

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn
 

Empfohlen (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

Chotto Dive into Object of JavaScript