in javascript learning & development telerik software academy

22
Inheritance with Classical OOP In JavaScript Learning & Development http://academy.telerik.com Telerik Software Academy

Upload: -

Post on 18-Jan-2018

225 views

Category:

Documents


0 download

DESCRIPTION

Like in C#, Java or C++

TRANSCRIPT

Page 1: In JavaScript Learning & Development  Telerik Software Academy

Inheritance with Classical OOP

In JavaScript

Learning & Developmenthttp://academy.telerik.com

Telerik Software Academy

Page 2: In JavaScript Learning & Development  Telerik Software Academy

Table of Contents

Implementing classical inheritance The prototype chain Using parent methods OOP frameworks

John Resig simple inheritance

Page 3: In JavaScript Learning & Development  Telerik Software Academy

Inheritance in Classical OOPLike in C#, Java or C++

Page 4: In JavaScript Learning & Development  Telerik Software Academy

Inheritance in Classical OOP Inheritance is a way to extend the

functionality of an object, into another object Like Student inherits Person Person inherits Mammal, etc…

In JavaScript inheritance is achieved by setting the prototype of the derived type to an instance of the super typefunction Person(fname, lname) {}function Student(fname, lname, grade) {}Student.prototype = new Person();

var student = new Student("Kiro", "Troikata", 7);

Now all instances of type Student are also of type Person and have Person functionality

Page 5: In JavaScript Learning & Development  Telerik Software Academy

Inheritance in Classical OOPLive Demo

Page 6: In JavaScript Learning & Development  Telerik Software Academy

The Prototype ChainThe way to search properties in

JavaScript

Page 7: In JavaScript Learning & Development  Telerik Software Academy

The Prototype Chain Objects in JavaScript can have only a single prototype Their prototype also has a

prototype, etc… This is called the prototype chain

When a property is called on an object1. This object is searched for the

property2. If the object does not contain such

property, its prototype is checked for the property, etc…

3. If a null prototype is reached, the result is undefined

Page 8: In JavaScript Learning & Development  Telerik Software Academy

Calling Parent MethodsUse some of the power of OOP

Page 9: In JavaScript Learning & Development  Telerik Software Academy

Calling Parent Methods

JavaScript has no direct way of calling its parent methods Function constructors actually does

not who or what is their parent Calling parent methods is done using call and apply

Page 10: In JavaScript Learning & Development  Telerik Software Academy

Calling Parent Methods: Example

Having Shape:var Shape = (function () { function Shape(x, y) { //initialize the shape } Shape.prototype = { serialize: function () { //serialize the shape //return the serialized } }; return Shape;}());

var Rect = (function () { function Rect(x, y, width, height) { Shape.call(this, x, y); //init the Rect } Rect.prototype = new Shape(); Rect.prototype.serialize=function (){ Shape.prototype .serialize .call(this); //add Rect specific serialization //return the serialized; }; return Rect;}());

Inheriting it with Rect

Page 11: In JavaScript Learning & Development  Telerik Software Academy

Calling Parent Methods: Example

Having Shape:var Shape = (function () { function Shape(x, y) { //initialize the shape } Shape.prototype = { serialize: function () { //serialize the shape //return the serialized } }; return Shape;}());

var Rect = (function () { function Rect(x, y, width, height) { Shape.call(this, x, y); //init the Rect } Rect.prototype = new Shape(); Rect.prototype.serialize=function (){ Shape.prototype .serialize .call(this); //add Rect specific serialization //return the serialized; }; return Rect;}());

Inheriting it with Rect

Call parent constructor

Page 12: In JavaScript Learning & Development  Telerik Software Academy

Calling Parent Methods: Example

Having Shape:var Shape = (function () { function Shape(x, y) { //initialize the shape } Shape.prototype = { serialize: function () { //serialize the shape //return the serialized } }; return Shape;}());

var Rect = (function () { function Rect(x, y, width, height) { Shape.call(this, x, y); //init the Rect } Rect.prototype = new Shape(); Rect.prototype.serialize=function (){ Shape.prototype .serialize .call(this); //add Rect specific serialization //return the serialized; }; return Rect;}());

Inheriting it with Rect

Call parent constructor

Call parent method

Page 13: In JavaScript Learning & Development  Telerik Software Academy

Calling Parent MethodsLive Demo

Page 14: In JavaScript Learning & Development  Telerik Software Academy

OOP Frameworks

Page 15: In JavaScript Learning & Development  Telerik Software Academy

OOP Frameworks OOP is a primary design paradigm in most programming languages Yet, OOP in JavaScript is not that

perfect And that is why every framework has its own way of doing OOP YUI, Prototype.js, Backbone.js, etc…

If none of these frameworks is used, a simple implementation by John Resig is intruded

Page 16: In JavaScript Learning & Development  Telerik Software Academy

var Shape = Class.extend({ init: function(x, y){ this._x = x; this._y = y; },

serialize: function(){ return { x: this._x, y: this._y }; }});

John Resig Simple Inheritance

How to use it?Copy the code from:http://

tinyurl.com/simple-inheritance Create "class"

with:Inherit it with:

var Rect = Shape.extend({ init: function(x, y, w, h){ this._super(x, y); this._width = w; this._height = h; }, serialize: function(){ var res = this._super(); res.width = this._width; res.height = this._height; return res; }});

Page 17: In JavaScript Learning & Development  Telerik Software Academy

var Shape = Class.extend({ init: function(x, y){ this._x = x; this._y = y; },

serialize: function(){ return { x: this._x, y: this._y }; }});

John Resig Simple Inheritance

How to use it?Copy the code from:http://

tinyurl.com/simple-inheritance Create "class"

with:Inherit it with:

var Rect = Shape.extend({ init: function(x, y, w, h){ this._super(x, y); this._width = w; this._height = h; }, serialize: function(){ var res = this._super(); res.width = this._width; res.height = this._height; return res; }});

Constructor

Page 18: In JavaScript Learning & Development  Telerik Software Academy

var Shape = Class.extend({ init: function(x, y){ this._x = x; this._y = y; },

serialize: function(){ return { x: this._x, y: this._y }; }});

John Resig Simple Inheritance

How to use it?Copy the code from:http://

tinyurl.com/simple-inheritance Create "class"

with:Inherit it with:

var Rect = Shape.extend({ init: function(x, y, w, h){ this._super(x, y); this._width = w; this._height = h; }, serialize: function(){ var res = this._super(); res.width = this._width; res.height = this._height; return res; }});

Method

Constructor

Page 19: In JavaScript Learning & Development  Telerik Software Academy

var Shape = Class.extend({ init: function(x, y){ this._x = x; this._y = y; },

serialize: function(){ return { x: this._x, y: this._y }; }});

John Resig Simple Inheritance

How to use it?Copy the code from:http://

tinyurl.com/simple-inheritance Create "class"

with:Inherit it with:

var Rect = Shape.extend({ init: function(x, y, w, h){ this._super(x, y); this._width = w; this._height = h; }, serialize: function(){ var res = this._super(); res.width = this._width; res.height = this._height; return res; }});

Method

Parent construc

tor

Constructor

Page 20: In JavaScript Learning & Development  Telerik Software Academy

var Shape = Class.extend({ init: function(x, y){ this._x = x; this._y = y; },

serialize: function(){ return { x: this._x, y: this._y }; }});

John Resig Simple Inheritance

How to use it?Copy the code from:http://

tinyurl.com/simple-inheritance Create "class"

with:Inherit it with:

var Rect = Shape.extend({ init: function(x, y, w, h){ this._super(x, y); this._width = w; this._height = h; }, serialize: function(){ var res = this._super(); res.width = this._width; res.height = this._height; return res; }});

Method

Parent construc

tor

Parent serialize method

Constructor

Page 21: In JavaScript Learning & Development  Telerik Software Academy

John Resig Simple Inheritance

Live Demo

Page 22: In JavaScript Learning & Development  Telerik Software Academy

форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно

програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки

уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop

уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC

безплатен курс "Разработка на софтуер в cloud среда"

BG Coder - онлайн състезателна система - online judge

курсове и уроци по програмиране, книги – безплатно от Наков

безплатен курс "Качествен програмен код"

алго академия – състезателно програмиране, състезанияASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NET

курсове и уроци по програмиране – Телерик академия

курс мобилни приложения с iPhone, Android, WP7, PhoneGapfree C# book, безплатна книга C#, книга Java, книга C# Дончо Минков - сайт за програмиране

Николай Костов - блог за програмиранеC# курс, програмиране, безплатно

?? ? ?

??? ?

?

? ?

??

?

?

? ?

Questions?

?

Inheritance in Classical OOP