+ All Categories
Home > Technology > 16. Advanced JavaScript - Web Front-End

16. Advanced JavaScript - Web Front-End

Date post: 22-Nov-2014
Category:
Upload: telerik-software-academy
View: 1,910 times
Download: 5 times
Share this document with a friend
Description:
JavaScript and Object Oriented Programming Telerik Software Academy: http://html5course.telerik.com The website and all video materials are in Bulgarian Content: JavaScript OOP - Constructors - Properties - Functions - Inheritance - Polymorphism - Extending Prebuilt JavaScript Objects Prototype Object Inheritance and Polymorphism in JavaScript Extending Prebuilt JavaScript Objects
18
Advanced %JavaScript Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training http://nakov.com http://html5course.telerik.com
Transcript
Page 1: 16. Advanced JavaScript - Web Front-End

Advanced %JavaScript

Svetlin Nakov

Telerik Software Academyacademy.telerik.com

Manager Technical Traininghttp://nakov.com

http://html5course.telerik.com

Page 2: 16. Advanced JavaScript - Web Front-End

Table of Contents JavaScript OOP

Constructors Properties Functions Inheritance Polymorphism Extending Prebuilt JavaScript

Objects

2

Page 3: 16. Advanced JavaScript - Web Front-End

JavaScript OOPProperties, Functions, Inheritance

Page 4: 16. Advanced JavaScript - Web Front-End

JavaScript OOP The current design of the JavaScript language, did not fully implement the object-oriented paradigms There are various implementations

of object-oriented programming techniques being used on the Web today

Primary goals of OOP Encapsulation Polymorphism Inheritance

4

Page 5: 16. Advanced JavaScript - Web Front-End

Defining Classes The simplest way is to use the built-

in Object data type In JavaScript, objects are

implemented as a collection of named properties (key-value pairs)

JavaScript allows the creation of any number of properties in an object at any time They are dynamic – do not have to be

pre-defined in an object declaration or constructor

5

var student = new Object;student.name= "Doncho Minkov";student.grade = 3;

Page 6: 16. Advanced JavaScript - Web Front-End

Defining a Class with Constructors

A new JavaScript class is defined by creating a function (serving as constructor) When used with the new operator, a

function serves as a constructor for that class

Internally, JavaScript creates an Object, and then calls the constructor function

6

function Student(){ this.name = "Doncho Minkov"; this.grade = 3;} var student = new Student;

Page 7: 16. Advanced JavaScript - Web Front-End

Defining a Class with Constructors (2)

When defining a constructor function, we can make many objects with the same properties

7

function Student(name, grade){ this.name = name; this.grade = grade;}

var doncho = new Student("Doncho Minkov", 3);var pesho = new Student("Pesho Peshov",2 );var stamat = new Student("Stamat Geshov",4);

Page 8: 16. Advanced JavaScript - Web Front-End

Class Functions We can add a functions (methods) to the class at any time

8

function Student(name, grade){ this.name = name; this.grade = grade; this.sayHello = function() { alert("Hi! I am " + this.name); }}

var doncho = new Student("Doncho Minkov", 3);doncho.sayHello();

defining-classes.html

Page 9: 16. Advanced JavaScript - Web Front-End

Prototype Object

Page 10: 16. Advanced JavaScript - Web Front-End

Object Prototypes We can use the prototype object to add custom properties / methods to classes That is reflected on all instances of

the class How to use the prototype object?

Simply reference the keyword prototype on the object before adding the custom property

10

function Circle() {}

Circle.prototype.pi = 3.14159;

Page 11: 16. Advanced JavaScript - Web Front-End

Object Prototypes (2) Adding a function to a class at runtime using the prototype object

11

function Circle() {}

Circle.prototype.pi = 3.14159;Circle.prototype.radius = 5;

Circle.prototype.calculateArea = function () { return this.pi * this.radius * 2;}

var circle = new Circle();var area = circle.calculateArea();alert(area); // 31.4159

prototype-object.html

Page 12: 16. Advanced JavaScript - Web Front-End

Prototype Object to Add Functionality to Build-in

Classes Dynamically add a function to a built-in class at runtime using the prototype object:

12

Array.prototype.showMax = function () { var max = this[0]; for (var i = 1; i < this.length; i++) { if (max < this[i]) { max = this[i]; } } return max; }

var array = new Array(9, 1, 11, 3, 4);var max = array.showMax();alert(max); // 11

Attaching a method to the Array class

Page 13: 16. Advanced JavaScript - Web Front-End

Inheritance and Polymorphism in

JavaScript

Page 14: 16. Advanced JavaScript - Web Front-End

Inheritance in JavaScript

To inherit a class in JavaScript you should set the prototype object of the subclass to the superclass class:

14

function Person(name) { this.name = name; this.talk = function () { alert("Hi! I am " + this.name); }}

function Student(name, grade) { this.name = name; this.grade = grade;}

Student.prototype = new Person();

This way we say that the Student class will have all

the functionality of the Person

class

inheritance.html

Page 15: 16. Advanced JavaScript - Web Front-End

Polymorphism in JavaScript

Polymorphism = ability to take more than one form (objects have more than one type) A class can be used through its

parent interface A child class may override some of

the behavior of the parent class

15

Student.prototype = new Person();Teacher.prototype = new Person();

var array = new Array( new Teacher("Gana","Math"), new Student("Gosho",3), new Person("Pesho"), new Teacher("Mara","Literature"));for (var i = 0; i < array.length; i++) { array[i].talk();}

polymorphism.html

Page 16: 16. Advanced JavaScript - Web Front-End

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

програмиране за деца – безплатни курсове и уроцибезплатен 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#Дончо Минков - сайт за програмиранеНиколай Костов - блог за програмиранеC# курс, програмиране, безплатно

?

? ? ??

?? ?

?

?

?

??

?

?

? ?

Questions?

?

Advanced %JavaScript

http://academy.telerik.com

Page 17: 16. Advanced JavaScript - Web Front-End

Exercises Implement a class Human, having

name, gender, address, telephone number It should have a methods for introducing

himself (ex. "Hi I am …!", "I am … years old!")

Implement classes Student and Parent inheriting the Human class A Student should have

State holding where s/he studies, a list of his/her marks

A method to count the average of their marks

A method for adding/removing a mark

A Parent should hold a list of all his children(Student objects) and a method to yell at a concrete of his children

17

Page 18: 16. Advanced JavaScript - Web Front-End

Free Trainings @ Telerik Academy

"Web Design with HTML 5, CSS 3 and JavaScript" course @ Telerik Academy html5course.telerik.com

Telerik Software Academy academy.telerik.com

Telerik Academy @ Facebook facebook.com/TelerikAcademy

Telerik Software Academy Forums forums.academy.telerik.com


Recommended