© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function Person(name, age) {
return {
name: name,
age : age,
sayName: function(){
console.log();
}
};
}
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function Person(name, age) {
return {
name,
age,
sayName(){
console.log();
}
};
}
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var lastName = "last name" ;
var person = {
lastName : lastName ,
"first name" : "Eyal" ,
[lastName] : "Vardi"
};
console.log(person[ "first name" ]); // "Eyal"
console.log(person[lastName]); // Vardi
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var lastName = "last name" ;
var person = {
"first name" : "Eyal" ,
[ lastName + "" ] : "Vardi"
};
console.log( person[ "first name" ] ); // "Eyal"
console.log( person[lastName + ""] ); // Vardi
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function equinox2() {
return { date: 20, month: "March", year: 2015,
time: { hour: 11, minute: 2 }
};
}
var { date: d, month: m, time : { hour: h} } = equinox2();
// 20 March at 11
console.log(d + " " + m + " at " + h);
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var [start, end] = ["earth", "moon"] // initialize
console.log(start + " => " + end); // earth => moon
[start, end] = [end, start] // swapping
console.log(start + " => " + end); // moon => earth
function equinox() { return [20, "March", 2013, 11, 02]; }
var [date, month, , ,] = equinox();
console.log( date +" "+ month); // 20 March
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function mixin(receiver, supplier) {
Object.keys(supplier).forEach( function(key) {
receiver[key] = supplier[key];
});
return receiver;
}
// == Object.assign(receiver, supplier);
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
new
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
alert(person1.constructor == Person); //true
alert(person2.constructor == Person); //true
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function SuperType(name){
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
SuperType.prototype.sayName = function(){ return this.name; };
function SubType(name, age){
SuperType.call(this, name);
this.age = age;
}
SubType.prototype = Object.create(SuperType.prototype);
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function(){
alert(this.age);
};
SubType.prototype.sayName = function(){
return SuperType.prototype.sayName.call(this) + "!!";
};
override
fix constructor
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
public class Person : BasePerson
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Person(string FirstName, string LastName) : base()
{
this.FirstName = FirstName;
this.LastName = LastName;
}
public string GetFullName(){
return FirstName + " " + LastName;
}
}
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
class Person extends BasePerson {
constructor(firstName, lastName) {
super();
this._firstName = firstName;
this._lastName = lastName;
}
getFullName(){
return this.firstName + " " + this.lastName;
}
get firstName() { return this._firstName; }
set firstName(value) { this._firstName = value; }
get lastName() { return this._lastName; }
set lastName(value) { this._lastName = value; }
}
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
SubType.prototype = Object.create(SuperType.prototype);
=
var objPro = {};
Object.setPrototypeOf( objPro , SuperType.prototype );
SubType.prototype = objPro;
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
// prototype is person
let friend = {
__proto__: person,
getGreeting() {
return super() + ", hi!" ;
}
};
function getGreeting() {
return super.getGreeting() + ", yo!" ;
}
console.log(friend.getGreeting()); // "Hello, hi!"
// assign getGreeting to the global function
var getFriendlyGreeting = getGreeting.toMethod(friend);
console.log(getFriendlyGreeting()); // "Hello, yo!"
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
let friend = {
__proto__: person,
getGreeting() {
// same as Object.getPrototypeOf(this).getGreeting.call(this)
// or this.__proto__.getGreeting.call(this)
// or super.getGreeting()
return super() + ", hi!" ;
}
};
 Determine the [[HomeObject]]
 Call Object.getPrototypeOf( [[HomeObject]] )
 Searched for a function with the same name as the executing function.
 Binding the “this” to the function.
 Execute the function.
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}
class Square extends Polygon {
constructor(length) {
super(length, length);
this.name = 'Square';
}
get area() {
return this.height * this.width;
}
}
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
http://www.2ality.com/
Understanding ECMAScript 6
http://ecmascript6.org/
A Few New Things Coming To JavaScript
HARMONY OF DREAMS COME TRUE
Harmony specification_drafts
© 2015 Eyal Vardi. All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
eyalvardi.wordpress.com

Objects & Classes in ECMAScript 6.0

  • 1.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 2.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] function Person(name, age) { return { name: name, age : age, sayName: function(){ console.log(); } }; }
  • 3.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] function Person(name, age) { return { name, age, sayName(){ console.log(); } }; }
  • 4.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] var lastName = "last name" ; var person = { lastName : lastName , "first name" : "Eyal" , [lastName] : "Vardi" }; console.log(person[ "first name" ]); // "Eyal" console.log(person[lastName]); // Vardi
  • 5.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] var lastName = "last name" ; var person = { "first name" : "Eyal" , [ lastName + "" ] : "Vardi" }; console.log( person[ "first name" ] ); // "Eyal" console.log( person[lastName + ""] ); // Vardi
  • 6.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] function equinox2() { return { date: 20, month: "March", year: 2015, time: { hour: 11, minute: 2 } }; } var { date: d, month: m, time : { hour: h} } = equinox2(); // 20 March at 11 console.log(d + " " + m + " at " + h);
  • 7.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] var [start, end] = ["earth", "moon"] // initialize console.log(start + " => " + end); // earth => moon [start, end] = [end, start] // swapping console.log(start + " => " + end); // moon => earth function equinox() { return [20, "March", 2013, 11, 02]; } var [date, month, , ,] = equinox(); console.log( date +" "+ month); // 20 March
  • 8.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] function mixin(receiver, supplier) { Object.keys(supplier).forEach( function(key) { receiver[key] = supplier[key]; }); return receiver; } // == Object.assign(receiver, supplier);
  • 9.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] new
  • 10.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] alert(person1.constructor == Person); //true alert(person2.constructor == Person); //true
  • 11.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 12.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] function SuperType(name){ this.name = name; this.colors = ['red', 'blue', 'green']; } SuperType.prototype.sayName = function(){ return this.name; }; function SubType(name, age){ SuperType.call(this, name); this.age = age; } SubType.prototype = Object.create(SuperType.prototype); SubType.prototype.constructor = SubType; SubType.prototype.sayAge = function(){ alert(this.age); }; SubType.prototype.sayName = function(){ return SuperType.prototype.sayName.call(this) + "!!"; }; override fix constructor
  • 13.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] public class Person : BasePerson { public string FirstName { get; set; } public string LastName { get; set; } public Person(string FirstName, string LastName) : base() { this.FirstName = FirstName; this.LastName = LastName; } public string GetFullName(){ return FirstName + " " + LastName; } }
  • 14.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] class Person extends BasePerson { constructor(firstName, lastName) { super(); this._firstName = firstName; this._lastName = lastName; } getFullName(){ return this.firstName + " " + this.lastName; } get firstName() { return this._firstName; } set firstName(value) { this._firstName = value; } get lastName() { return this._lastName; } set lastName(value) { this._lastName = value; } }
  • 15.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] SubType.prototype = Object.create(SuperType.prototype); = var objPro = {}; Object.setPrototypeOf( objPro , SuperType.prototype ); SubType.prototype = objPro;
  • 16.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected]
  • 17.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] // prototype is person let friend = { __proto__: person, getGreeting() { return super() + ", hi!" ; } }; function getGreeting() { return super.getGreeting() + ", yo!" ; } console.log(friend.getGreeting()); // "Hello, hi!" // assign getGreeting to the global function var getFriendlyGreeting = getGreeting.toMethod(friend); console.log(getFriendlyGreeting()); // "Hello, yo!"
  • 18.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] let friend = { __proto__: person, getGreeting() { // same as Object.getPrototypeOf(this).getGreeting.call(this) // or this.__proto__.getGreeting.call(this) // or super.getGreeting() return super() + ", hi!" ; } };  Determine the [[HomeObject]]  Call Object.getPrototypeOf( [[HomeObject]] )  Searched for a function with the same name as the executing function.  Binding the “this” to the function.  Execute the function.
  • 19.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] class Polygon { constructor(height, width) { this.height = height; this.width = width; } } class Square extends Polygon { constructor(length) { super(length, length); this.name = 'Square'; } get area() { return this.height * this.width; } }
  • 20.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] http://www.2ality.com/ Understanding ECMAScript 6 http://ecmascript6.org/ A Few New Things Coming To JavaScript HARMONY OF DREAMS COME TRUE Harmony specification_drafts
  • 21.
    © 2015 EyalVardi. All rights reserved. Tel: 054-5-767-300, Email: [email protected] eyalvardi.wordpress.com