SlideShare a Scribd company logo
JavaScript
Object-Oriented
Programming
Piotr Czajkowski
piotr.czajkowski@blstream.com
constructor
new
this
bind
class
constructor declaration
var Person = function (name) {
this.name = name;
};
Person.prototype.sayHi = function () {
return "Hi, I'm " + this.name;
};
var me = new Person("Piotr");
me.sayHi(); // “Hi I'm Piotr”
constructor inheritance
var Programmer = function (name, language) {
Person.call(this, name);
this.language = language;
}
Programmer.prototype = Object.create(Person.prototype);
Programmer.prototype.constructor = Programmer;
constructor prototype chain
var Car = function () {
this.name = "Car";
}
var Truck = function () {};
Truck.prototype = new Car();
var t = new Truck();
t; // Object { name="Car"}
t.name; // "Car"
t.hasOwnProperty("name"); // false
t.name = "Truck";
t.hasOwnProperty("name"); // true
constructor Object.defineProperty()
Object.defineProperty(obj, "name", {
configurable : false,
enumerable : false,
// data descriptors
writable: false,
value : "Object value",
// accessor descriptors
get : function () {},
set : function (val) {}
});
var o = {};
Object.defineProperty(o, "name", {
value : "Object O"
});
o.name; // Object O
o.name = "New name";
o.name; // Object O
new creating objects
var Person = function (name) {
this.name = name;
};
var me = new Person("Piotr");
me.name; // “Piotr”
var me = Person("Piotr");
me; // undefined
window.name; // “Piotr”
new instanceof
var Person = function () {
this.name = “Person”;
};
var p = new Person();
p instanceof Person; // true
"Hello" instanceof String; // false
new String("Hello") instanceof String;
// true
new creating objects
var Person = function () {
this.name = “Person”;
};
var p = new Person();
p.name; // “Person”
p instanceof Person; // true
var Person = function () {
return {
name : “Person”
};
};
var p = new Person();
p.name; // “Person”
p instanceof Person; // false
this function context
function getThis() { return this; }
getThis(); // window
var o = { name : "Object O" };
o.getThis = getThis;
o.getThis();
// Object { name="Object O",
getThis=getThis()}
var o = {
name : "Object O",
getThis : function () { return this;}
};
o.getThis(); // Object
var getThis = o.getThis;
getThis(); // window
this inner functions
var o = {
innerThis : function () {
function getThis () {
return this;
};
return getThis();
}
};
o.innerThis(); // window
this strict mode
function getThis () {
"use strict";
return this;
};
getThis(); // undefined
var o = {
innerThis : function () {
"use strict";
function getThis () {
return this;
};
return getThis();
}
};
o.innerThis(); // undefined
bind changing function context
var o = { name : "Object O" };
function getThis () { return this; };
getThis(); // window
var boundGetThis = getThis.bind(o);
boundGetThis();
// Object { name="Object O"}
var o = { name : "Object O" };
function getThis() { return this; }
getThis(); // window
o.getThis = getThis;
o.getThis();
// Object { name="Object O",
getThis=getThis()}
bind callbacks (self, that, _this)
var o = {
fetch : function () {
function getThis () {
console.log(this);
};
$.ajax({ url : "url" })
.done(getThis);
}
};
o.fetch(); // ajax settings object
var o = {
fetch : function () {
function getThis () {
console.log(this);
};
$.ajax({ url : "url" })
.done(getThis.bind(this));
}
};
o.fetch(); // Object { fetch=function() }
bind to bind or not to bind
function onClick () {
console.log("Click");
};
$("body").on("click", onClick);
$("body").trigger("click"); // “Click”
$("body").off("click", onClick);
$("body").trigger("click");
function onClick () {
console.log("Click");
};
$("body").on("click", onClick.bind());
$("body").trigger("click"); // “Click”
$("body").off("click",???);
ES6 class syntactic sugar
class Person {
constructor(name) {
this.name = name;
}
describe() {
return this.name;
}
}
function Person(name) {
this.name = name;
};
Person.prototype.describe =
function () {
return this.name;
};
ES6 class inheritance
class Employee extends Person {
constructor(name, title) {
super.constructor(name);
this.title = title;
}
}
function Employee(name, title) {
Person.call(this, name);
this.title = title;
}
Employee.prototype =
Object.create(Person.prototype);
Employee.prototype.constructor =
Employee;
ES6 Object Literal Property Value Shorthand
var a = "Test",
b = 42,
c = {};
var oldObj = {
a : a,
b : b,
c : c
};
var es6Obj = { a, b, c };
var name = “Object”;
var o = {
name,
get name() {},
set name(value) {},
getThis() { return this; },
};
ES6 Computed Property Names
var i = 0,
param = “name”;
var obj = {
[param] : “Object O”,
[“value” + ++i] : i,
};
var obj = {};
obj[param] = “Object O”;
obj[“value” + ++i] = i;
ES6 Arrow Functions
var foo = () => { return this; };
foo(); // window
var bar = () => {
“use strict”;
return this;
};
bar(); // window
var o = {
fetch : function () {
$.ajax({ url : "url" })
.done(() => {
console.log(this);
});
}
};
o.fetch(); // Object { fetch=function() }
constructor
new
this
bind
class
Thank You
Piotr Czajkowski
piotr.czajkowski@blstream.com

More Related Content

What's hot (20)

Sumahexavector
SumahexavectorSumahexavector
Sumahexavector
jbersosa
 
Acessardados Aula7
Acessardados Aula7Acessardados Aula7
Acessardados Aula7
softeam
 
Simular un next del recordset en php de forma rudimentaria
Simular un next del recordset en php de forma rudimentariaSimular un next del recordset en php de forma rudimentaria
Simular un next del recordset en php de forma rudimentaria
jbersosa
 
es6.concurrency()
es6.concurrency()es6.concurrency()
es6.concurrency()
Ingvar Stepanyan
 
ECMA2015 INSIDE
ECMA2015 INSIDEECMA2015 INSIDE
ECMA2015 INSIDE
Jun Ho Lee
 
jQuery for designers
jQuery for designersjQuery for designers
jQuery for designers
Johan Ronsse
 
RxSwift 예제로 감잡기
RxSwift 예제로 감잡기RxSwift 예제로 감잡기
RxSwift 예제로 감잡기
Yongha Yoo
 
Testování prakticky
Testování praktickyTestování prakticky
Testování prakticky
Filip Procházka
 
PHP WTF
PHP WTFPHP WTF
PHP WTF
markstory
 
Modern Mobile Web Apps
Modern Mobile Web AppsModern Mobile Web Apps
Modern Mobile Web Apps
dynamis
 
modern javascript, unobtrusive javascript, jquery
modern javascript, unobtrusive javascript, jquerymodern javascript, unobtrusive javascript, jquery
modern javascript, unobtrusive javascript, jquery
Adam Zygadlewicz
 
Palestra sobre MongoDB com PHP no PHP'n'Rio
Palestra sobre MongoDB com PHP no PHP'n'Rio Palestra sobre MongoDB com PHP no PHP'n'Rio
Palestra sobre MongoDB com PHP no PHP'n'Rio
Suissa
 
Silex. Микрофреймворк для микроприложений
Silex. Микрофреймворк для микроприложенийSilex. Микрофреймворк для микроприложений
Silex. Микрофреймворк для микроприложений
Softline
 
Object Oriented Lies
Object Oriented LiesObject Oriented Lies
Object Oriented Lies
Yegor Bugayenko
 
jQuery プラグインの作り方
jQuery プラグインの作り方jQuery プラグインの作り方
jQuery プラグインの作り方
Takeru Suzuki
 
Sumahexavector
SumahexavectorSumahexavector
Sumahexavector
jbersosa
 
Acessardados Aula7
Acessardados Aula7Acessardados Aula7
Acessardados Aula7
softeam
 
Simular un next del recordset en php de forma rudimentaria
Simular un next del recordset en php de forma rudimentariaSimular un next del recordset en php de forma rudimentaria
Simular un next del recordset en php de forma rudimentaria
jbersosa
 
ECMA2015 INSIDE
ECMA2015 INSIDEECMA2015 INSIDE
ECMA2015 INSIDE
Jun Ho Lee
 
jQuery for designers
jQuery for designersjQuery for designers
jQuery for designers
Johan Ronsse
 
RxSwift 예제로 감잡기
RxSwift 예제로 감잡기RxSwift 예제로 감잡기
RxSwift 예제로 감잡기
Yongha Yoo
 
Modern Mobile Web Apps
Modern Mobile Web AppsModern Mobile Web Apps
Modern Mobile Web Apps
dynamis
 
modern javascript, unobtrusive javascript, jquery
modern javascript, unobtrusive javascript, jquerymodern javascript, unobtrusive javascript, jquery
modern javascript, unobtrusive javascript, jquery
Adam Zygadlewicz
 
Palestra sobre MongoDB com PHP no PHP'n'Rio
Palestra sobre MongoDB com PHP no PHP'n'Rio Palestra sobre MongoDB com PHP no PHP'n'Rio
Palestra sobre MongoDB com PHP no PHP'n'Rio
Suissa
 
Silex. Микрофреймворк для микроприложений
Silex. Микрофреймворк для микроприложенийSilex. Микрофреймворк для микроприложений
Silex. Микрофреймворк для микроприложений
Softline
 
jQuery プラグインの作り方
jQuery プラグインの作り方jQuery プラグインの作り方
jQuery プラグインの作り方
Takeru Suzuki
 

Viewers also liked (20)

java script functions, classes
java script functions, classesjava script functions, classes
java script functions, classes
Vijay Kalyan
 
2ndQuarter2ndMeeting(formatting number)
2ndQuarter2ndMeeting(formatting number)2ndQuarter2ndMeeting(formatting number)
2ndQuarter2ndMeeting(formatting number)
Esmeraldo Jr Guimbarda
 
Week 5 java script functions
Week 5  java script functionsWeek 5  java script functions
Week 5 java script functions
brianjihoonlee
 
2java Oop
2java Oop2java Oop
2java Oop
Adil Jafri
 
Lambda functions in java 8
Lambda functions in java 8Lambda functions in java 8
Lambda functions in java 8
James Brown
 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting Lambdas
Ganesh Samarthyam
 
TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)
Esmeraldo Jr Guimbarda
 
Functional Programming in Java
Functional Programming in JavaFunctional Programming in Java
Functional Programming in Java
Narendran Solai Sridharan
 
02 java programming basic
02  java programming basic02  java programming basic
02 java programming basic
Zeeshan-Shaikh
 
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Philip Schwarz
 
Fp java8
Fp java8Fp java8
Fp java8
Yanai Franchi
 
Functions in javascript
Functions in javascriptFunctions in javascript
Functions in javascript
baabtra.com - No. 1 supplier of quality freshers
 
JavaScript Functions
JavaScript Functions JavaScript Functions
JavaScript Functions
Reem Alattas
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
Talha Ocakçı
 
Functional Javascript
Functional JavascriptFunctional Javascript
Functional Javascript
guest4d57e6
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
Colin DeCarlo
 
Functional programming in java
Functional programming in javaFunctional programming in java
Functional programming in java
John Ferguson Smart Limited
 
Programming
ProgrammingProgramming
Programming
Sean Chia
 
Basic java for Android Developer
Basic java for Android DeveloperBasic java for Android Developer
Basic java for Android Developer
Nattapong Tonprasert
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In Java
Andrei Solntsev
 
java script functions, classes
java script functions, classesjava script functions, classes
java script functions, classes
Vijay Kalyan
 
2ndQuarter2ndMeeting(formatting number)
2ndQuarter2ndMeeting(formatting number)2ndQuarter2ndMeeting(formatting number)
2ndQuarter2ndMeeting(formatting number)
Esmeraldo Jr Guimbarda
 
Week 5 java script functions
Week 5  java script functionsWeek 5  java script functions
Week 5 java script functions
brianjihoonlee
 
Lambda functions in java 8
Lambda functions in java 8Lambda functions in java 8
Lambda functions in java 8
James Brown
 
Functional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting LambdasFunctional Programming in Java 8 - Exploiting Lambdas
Functional Programming in Java 8 - Exploiting Lambdas
Ganesh Samarthyam
 
TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)TM 2nd qtr-3ndmeeting(java script-functions)
TM 2nd qtr-3ndmeeting(java script-functions)
Esmeraldo Jr Guimbarda
 
02 java programming basic
02  java programming basic02  java programming basic
02 java programming basic
Zeeshan-Shaikh
 
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Understanding Java 8 Lambdas and Streams - Part 1 - Lambda Calculus, Lambda...
Philip Schwarz
 
JavaScript Functions
JavaScript Functions JavaScript Functions
JavaScript Functions
Reem Alattas
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
Talha Ocakçı
 
Functional Javascript
Functional JavascriptFunctional Javascript
Functional Javascript
guest4d57e6
 
JavaScript Functions
JavaScript FunctionsJavaScript Functions
JavaScript Functions
Colin DeCarlo
 
Functional Programming In Java
Functional Programming In JavaFunctional Programming In Java
Functional Programming In Java
Andrei Solntsev
 
Ad

More from intive (20)

Rok z Android MVVM
Rok z Android MVVMRok z Android MVVM
Rok z Android MVVM
intive
 
Don't Forget About the Layout
Don't Forget About the LayoutDon't Forget About the Layout
Don't Forget About the Layout
intive
 
You Don't Need Dependency Injection
You Don't Need Dependency InjectionYou Don't Need Dependency Injection
You Don't Need Dependency Injection
intive
 
OWASP Open SAMM
OWASP Open SAMMOWASP Open SAMM
OWASP Open SAMM
intive
 
Porównanie architektur MVVM i MVC (iOS)
Porównanie architektur MVVM i MVC (iOS)Porównanie architektur MVVM i MVC (iOS)
Porównanie architektur MVVM i MVC (iOS)
intive
 
Wprowadzenie do CoreBluetooth
Wprowadzenie do CoreBluetoothWprowadzenie do CoreBluetooth
Wprowadzenie do CoreBluetooth
intive
 
.Net anywhere
.Net anywhere.Net anywhere
.Net anywhere
intive
 
Front end - advanced development for beginners
Front end - advanced development for beginnersFront end - advanced development for beginners
Front end - advanced development for beginners
intive
 
Kotlin, Spek and tests
Kotlin, Spek and testsKotlin, Spek and tests
Kotlin, Spek and tests
intive
 
Patronage 2016 Windows 10 Warsztaty
Patronage 2016 Windows 10 WarsztatyPatronage 2016 Windows 10 Warsztaty
Patronage 2016 Windows 10 Warsztaty
intive
 
Techniczna organizacja zespołu cz 2
Techniczna organizacja zespołu cz 2Techniczna organizacja zespołu cz 2
Techniczna organizacja zespołu cz 2
intive
 
Techniczna organizacja zespołu
Techniczna organizacja zespołuTechniczna organizacja zespołu
Techniczna organizacja zespołu
intive
 
Organizacja zespołu
Organizacja zespołuOrganizacja zespołu
Organizacja zespołu
intive
 
Nie tylko C# - Ekosystem Microsoft dla programistów
Nie tylko C# - Ekosystem Microsoft dla programistówNie tylko C# - Ekosystem Microsoft dla programistów
Nie tylko C# - Ekosystem Microsoft dla programistów
intive
 
[PL] MVP do MVVM - separacja warstw w aplikacji androidowej
[PL] MVP do MVVM - separacja warstw w aplikacji androidowej[PL] MVP do MVVM - separacja warstw w aplikacji androidowej
[PL] MVP do MVVM - separacja warstw w aplikacji androidowej
intive
 
Tips & Tricks Android
Tips & Tricks AndroidTips & Tricks Android
Tips & Tricks Android
intive
 
Apple Watch - Getting Started
Apple Watch - Getting StartedApple Watch - Getting Started
Apple Watch - Getting Started
intive
 
Clean architecture: Android
Clean architecture: AndroidClean architecture: Android
Clean architecture: Android
intive
 
CoreLocation (iOS) in details
CoreLocation (iOS) in detailsCoreLocation (iOS) in details
CoreLocation (iOS) in details
intive
 
Developer Job in Practice
Developer Job in PracticeDeveloper Job in Practice
Developer Job in Practice
intive
 
Rok z Android MVVM
Rok z Android MVVMRok z Android MVVM
Rok z Android MVVM
intive
 
Don't Forget About the Layout
Don't Forget About the LayoutDon't Forget About the Layout
Don't Forget About the Layout
intive
 
You Don't Need Dependency Injection
You Don't Need Dependency InjectionYou Don't Need Dependency Injection
You Don't Need Dependency Injection
intive
 
OWASP Open SAMM
OWASP Open SAMMOWASP Open SAMM
OWASP Open SAMM
intive
 
Porównanie architektur MVVM i MVC (iOS)
Porównanie architektur MVVM i MVC (iOS)Porównanie architektur MVVM i MVC (iOS)
Porównanie architektur MVVM i MVC (iOS)
intive
 
Wprowadzenie do CoreBluetooth
Wprowadzenie do CoreBluetoothWprowadzenie do CoreBluetooth
Wprowadzenie do CoreBluetooth
intive
 
.Net anywhere
.Net anywhere.Net anywhere
.Net anywhere
intive
 
Front end - advanced development for beginners
Front end - advanced development for beginnersFront end - advanced development for beginners
Front end - advanced development for beginners
intive
 
Kotlin, Spek and tests
Kotlin, Spek and testsKotlin, Spek and tests
Kotlin, Spek and tests
intive
 
Patronage 2016 Windows 10 Warsztaty
Patronage 2016 Windows 10 WarsztatyPatronage 2016 Windows 10 Warsztaty
Patronage 2016 Windows 10 Warsztaty
intive
 
Techniczna organizacja zespołu cz 2
Techniczna organizacja zespołu cz 2Techniczna organizacja zespołu cz 2
Techniczna organizacja zespołu cz 2
intive
 
Techniczna organizacja zespołu
Techniczna organizacja zespołuTechniczna organizacja zespołu
Techniczna organizacja zespołu
intive
 
Organizacja zespołu
Organizacja zespołuOrganizacja zespołu
Organizacja zespołu
intive
 
Nie tylko C# - Ekosystem Microsoft dla programistów
Nie tylko C# - Ekosystem Microsoft dla programistówNie tylko C# - Ekosystem Microsoft dla programistów
Nie tylko C# - Ekosystem Microsoft dla programistów
intive
 
[PL] MVP do MVVM - separacja warstw w aplikacji androidowej
[PL] MVP do MVVM - separacja warstw w aplikacji androidowej[PL] MVP do MVVM - separacja warstw w aplikacji androidowej
[PL] MVP do MVVM - separacja warstw w aplikacji androidowej
intive
 
Tips & Tricks Android
Tips & Tricks AndroidTips & Tricks Android
Tips & Tricks Android
intive
 
Apple Watch - Getting Started
Apple Watch - Getting StartedApple Watch - Getting Started
Apple Watch - Getting Started
intive
 
Clean architecture: Android
Clean architecture: AndroidClean architecture: Android
Clean architecture: Android
intive
 
CoreLocation (iOS) in details
CoreLocation (iOS) in detailsCoreLocation (iOS) in details
CoreLocation (iOS) in details
intive
 
Developer Job in Practice
Developer Job in PracticeDeveloper Job in Practice
Developer Job in Practice
intive
 
Ad

Java Script - Object-Oriented Programming

  • 3. constructor declaration var Person = function (name) { this.name = name; }; Person.prototype.sayHi = function () { return "Hi, I'm " + this.name; }; var me = new Person("Piotr"); me.sayHi(); // “Hi I'm Piotr”
  • 4. constructor inheritance var Programmer = function (name, language) { Person.call(this, name); this.language = language; } Programmer.prototype = Object.create(Person.prototype); Programmer.prototype.constructor = Programmer;
  • 5. constructor prototype chain var Car = function () { this.name = "Car"; } var Truck = function () {}; Truck.prototype = new Car(); var t = new Truck(); t; // Object { name="Car"} t.name; // "Car" t.hasOwnProperty("name"); // false t.name = "Truck"; t.hasOwnProperty("name"); // true
  • 6. constructor Object.defineProperty() Object.defineProperty(obj, "name", { configurable : false, enumerable : false, // data descriptors writable: false, value : "Object value", // accessor descriptors get : function () {}, set : function (val) {} }); var o = {}; Object.defineProperty(o, "name", { value : "Object O" }); o.name; // Object O o.name = "New name"; o.name; // Object O
  • 7. new creating objects var Person = function (name) { this.name = name; }; var me = new Person("Piotr"); me.name; // “Piotr” var me = Person("Piotr"); me; // undefined window.name; // “Piotr”
  • 8. new instanceof var Person = function () { this.name = “Person”; }; var p = new Person(); p instanceof Person; // true "Hello" instanceof String; // false new String("Hello") instanceof String; // true
  • 9. new creating objects var Person = function () { this.name = “Person”; }; var p = new Person(); p.name; // “Person” p instanceof Person; // true var Person = function () { return { name : “Person” }; }; var p = new Person(); p.name; // “Person” p instanceof Person; // false
  • 10. this function context function getThis() { return this; } getThis(); // window var o = { name : "Object O" }; o.getThis = getThis; o.getThis(); // Object { name="Object O", getThis=getThis()} var o = { name : "Object O", getThis : function () { return this;} }; o.getThis(); // Object var getThis = o.getThis; getThis(); // window
  • 11. this inner functions var o = { innerThis : function () { function getThis () { return this; }; return getThis(); } }; o.innerThis(); // window
  • 12. this strict mode function getThis () { "use strict"; return this; }; getThis(); // undefined var o = { innerThis : function () { "use strict"; function getThis () { return this; }; return getThis(); } }; o.innerThis(); // undefined
  • 13. bind changing function context var o = { name : "Object O" }; function getThis () { return this; }; getThis(); // window var boundGetThis = getThis.bind(o); boundGetThis(); // Object { name="Object O"} var o = { name : "Object O" }; function getThis() { return this; } getThis(); // window o.getThis = getThis; o.getThis(); // Object { name="Object O", getThis=getThis()}
  • 14. bind callbacks (self, that, _this) var o = { fetch : function () { function getThis () { console.log(this); }; $.ajax({ url : "url" }) .done(getThis); } }; o.fetch(); // ajax settings object var o = { fetch : function () { function getThis () { console.log(this); }; $.ajax({ url : "url" }) .done(getThis.bind(this)); } }; o.fetch(); // Object { fetch=function() }
  • 15. bind to bind or not to bind function onClick () { console.log("Click"); }; $("body").on("click", onClick); $("body").trigger("click"); // “Click” $("body").off("click", onClick); $("body").trigger("click"); function onClick () { console.log("Click"); }; $("body").on("click", onClick.bind()); $("body").trigger("click"); // “Click” $("body").off("click",???);
  • 16. ES6 class syntactic sugar class Person { constructor(name) { this.name = name; } describe() { return this.name; } } function Person(name) { this.name = name; }; Person.prototype.describe = function () { return this.name; };
  • 17. ES6 class inheritance class Employee extends Person { constructor(name, title) { super.constructor(name); this.title = title; } } function Employee(name, title) { Person.call(this, name); this.title = title; } Employee.prototype = Object.create(Person.prototype); Employee.prototype.constructor = Employee;
  • 18. ES6 Object Literal Property Value Shorthand var a = "Test", b = 42, c = {}; var oldObj = { a : a, b : b, c : c }; var es6Obj = { a, b, c }; var name = “Object”; var o = { name, get name() {}, set name(value) {}, getThis() { return this; }, };
  • 19. ES6 Computed Property Names var i = 0, param = “name”; var obj = { [param] : “Object O”, [“value” + ++i] : i, }; var obj = {}; obj[param] = “Object O”; obj[“value” + ++i] = i;
  • 20. ES6 Arrow Functions var foo = () => { return this; }; foo(); // window var bar = () => { “use strict”; return this; }; bar(); // window var o = { fetch : function () { $.ajax({ url : "url" }) .done(() => { console.log(this); }); } }; o.fetch(); // Object { fetch=function() }