Download presentation
Presentation is loading. Please wait.
1
In JavaScript Learning & Development http://academy.telerik.com Telerik Software Academy
2
Implementing classical inheritance The prototype chain Using parent methods OOP frameworks John Resig simple inheritance
3
Like in C#, Java or C++
4
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 type function 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
5
Live Demo
6
The way to search properties in JavaScript
7
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 object 1.This object is searched for the property 2.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
8
Use some of the power of OOP
9
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
10
Having Shape: var Shape = (function () { function Shape(x, y) { function Shape(x, y) { //initialize the shape //initialize the shape } Shape.prototype = { Shape.prototype = { serialize: function () { serialize: function () { //serialize the shape //return the serialized //serialize the shape //return the serialized } }; }; return Shape; return Shape;}()); var Rect = (function () { function Rect(x, y, function Rect(x, y, width, height) { width, height) { Shape.call(this, x, y); Shape.call(this, x, y); //init the Rect //init the Rect } Rect.prototype = new Shape(); Rect.prototype = new Shape(); Rect.prototype.serialize=function (){ Rect.prototype.serialize=function (){ Shape.prototype Shape.prototype.serialize.serialize.call(this);.call(this); //add Rect specific serialization //add Rect specific serialization //return the serialized; //return the serialized; }; }; return Rect; return Rect;}()); Inheriting it with Rect
11
Having Shape: var Shape = (function () { function Shape(x, y) { function Shape(x, y) { //initialize the shape //initialize the shape } Shape.prototype = { Shape.prototype = { serialize: function () { serialize: function () { //serialize the shape //return the serialized //serialize the shape //return the serialized } }; }; return Shape; return Shape;}()); var Rect = (function () { function Rect(x, y, function Rect(x, y, width, height) { width, height) { Shape.call(this, x, y); Shape.call(this, x, y); //init the Rect //init the Rect } Rect.prototype = new Shape(); Rect.prototype = new Shape(); Rect.prototype.serialize=function (){ Rect.prototype.serialize=function (){ Shape.prototype Shape.prototype.serialize.serialize.call(this);.call(this); //add Rect specific serialization //add Rect specific serialization //return the serialized; //return the serialized; }; }; return Rect; return Rect;}()); Inheriting it with Rect Call parent constructor
12
Having Shape: var Shape = (function () { function Shape(x, y) { function Shape(x, y) { //initialize the shape //initialize the shape } Shape.prototype = { Shape.prototype = { serialize: function () { serialize: function () { //serialize the shape //return the serialized //serialize the shape //return the serialized } }; }; return Shape; return Shape;}()); var Rect = (function () { function Rect(x, y, function Rect(x, y, width, height) { width, height) { Shape.call(this, x, y); Shape.call(this, x, y); //init the Rect //init the Rect } Rect.prototype = new Shape(); Rect.prototype = new Shape(); Rect.prototype.serialize=function (){ Rect.prototype.serialize=function (){ Shape.prototype Shape.prototype.serialize.serialize.call(this);.call(this); //add Rect specific serialization //add Rect specific serialization //return the serialized; //return the serialized; }; }; return Rect; return Rect;}()); Inheriting it with Rect Call parent constructor Call parent method
13
Live Demo
15
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
16
var Shape = Class.extend({ init: function(x, y){ init: function(x, y){ this._x = x; this._x = x; this._y = y; this._y = y; }, }, serialize: function(){ serialize: function(){ return { return { x: this._x, x: this._x, y: this._y y: this._y }; }; }}); 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){ init: function(x, y, w, h){ this._super(x, y); this._super(x, y); this._width = w; this._width = w; this._height = h; this._height = h; }, }, serialize: function(){ serialize: function(){ var res = this._super(); var res = this._super(); res.width = this._width; res.width = this._width; res.height = this._height; res.height = this._height; return res; return res; }});
17
var Shape = Class.extend({ init: function(x, y){ init: function(x, y){ this._x = x; this._x = x; this._y = y; this._y = y; }, }, serialize: function(){ serialize: function(){ return { return { x: this._x, x: this._x, y: this._y y: this._y }; }; }}); 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){ init: function(x, y, w, h){ this._super(x, y); this._super(x, y); this._width = w; this._width = w; this._height = h; this._height = h; }, }, serialize: function(){ serialize: function(){ var res = this._super(); var res = this._super(); res.width = this._width; res.width = this._width; res.height = this._height; res.height = this._height; return res; return res; }}); Constructor
18
var Shape = Class.extend({ init: function(x, y){ init: function(x, y){ this._x = x; this._x = x; this._y = y; this._y = y; }, }, serialize: function(){ serialize: function(){ return { return { x: this._x, x: this._x, y: this._y y: this._y }; }; }}); 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){ init: function(x, y, w, h){ this._super(x, y); this._super(x, y); this._width = w; this._width = w; this._height = h; this._height = h; }, }, serialize: function(){ serialize: function(){ var res = this._super(); var res = this._super(); res.width = this._width; res.width = this._width; res.height = this._height; res.height = this._height; return res; return res; }}); Method Constructor
19
var Shape = Class.extend({ init: function(x, y){ init: function(x, y){ this._x = x; this._x = x; this._y = y; this._y = y; }, }, serialize: function(){ serialize: function(){ return { return { x: this._x, x: this._x, y: this._y y: this._y }; }; }}); 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){ init: function(x, y, w, h){ this._super(x, y); this._super(x, y); this._width = w; this._width = w; this._height = h; this._height = h; }, }, serialize: function(){ serialize: function(){ var res = this._super(); var res = this._super(); res.width = this._width; res.width = this._width; res.height = this._height; res.height = this._height; return res; return res; }}); Method Parent constructor Constructor
20
var Shape = Class.extend({ init: function(x, y){ init: function(x, y){ this._x = x; this._x = x; this._y = y; this._y = y; }, }, serialize: function(){ serialize: function(){ return { return { x: this._x, x: this._x, y: this._y y: this._y }; }; }}); 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){ init: function(x, y, w, h){ this._super(x, y); this._super(x, y); this._width = w; this._width = w; this._height = h; this._height = h; }, }, serialize: function(){ serialize: function(){ var res = this._super(); var res = this._super(); res.width = this._width; res.width = this._width; res.height = this._height; res.height = this._height; return res; return res; }}); Method Parent constructor Parent serialize method Constructor
21
Live Demo
22
форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен 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, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.