Download presentation
Presentation is loading. Please wait.
Published byJean Maxwell Modified over 9 years ago
1
Object Oriented Programming in JavaScript Akhilesh Bajaj The University of Tulsa MIS3023 All slides© 2015 Akhilesh Bajaj Slide 1
2
Instantiation Allows us to create multiple objects using a single class. In JavaScript there is no class keyword, instead we just use functions to create classes. Think of a class as a recipe to create new objects. We can create many objects of the same class, and each of these objects will get the same data fields and the same methods (or object functions). Once we create an object, we can use it in the code, and change the values in its fields. This way, over time, we can have multiple objects that start with the same recipe, but evolve differently in the code. Slide 2
3
Instantiation function Customer() { var name = "XXXXX"; var address = "XXXXX var gold = false; this.getName = function(){ return name; } this.getAddress =function() { return address; } this.getGold = function() { return gold; } Slide 3 Name of class Object fields -> Object method ->
4
Instantiation alert("In main script"); //create 3 objects of class Customer INSTANTIATION var bob = new Customer(); var john = new Customer(); var irene = new Customer(); alert(bob.getName()); Slide 4 <- Creating a new object of class Customer <- Calling a method on one of the objects that were created
5
Encapsulation We want the data fields in the objects to be private. In other words, we do not want these fields to be directly accessed in the code outside the class code. We want to provide methods that can be used by code outside the class to get and set the data fields. But again, we do NOT want the outside code to touch the data directly. The publicly available methods that can be called on by the outside code constitute the interface of the class. This interface is usually not changed once the class is designed. A public interface of methods, with private data fields is called encapsulation. This has several advantages: we can change the way the data is represented in the class, without altering the interface. This will not affect any of the outside code. This leads to more bug-free code since it reduces the coupling between different code modules. Slide 5
6
Encapsulation Murach’s JavaScript, C11© 2009, Mike Murach & Associates, Inc. Slide 6 <- get and set methods for each object field <- output method that prints the object fields
7
Encapsulation Slide 7 <- outside code that calls methods on the different objects.
8
Using objects in other objects We can use objects in an array, by creating an array of objects. Slide 8
9
Using objects in other objects Slide 9 function Customer(){ var name1 = "XXXX"; this.getName1 = function(){ return name1;} this.setname1 = function(n){ name1=n;} }//Customer function Customers(size){ var custs = new Array(size); initCusts(); function initCusts() { for(var i=0; i alert("In main script"); var myCusts = new Customers(5); //myCusts.initCusts(); myCusts.outputCusts();
10
Fun In Class Assignment Murach’s JavaScript, C11© 2009, Mike Murach & Associates, Inc. Slide 10 Create a class called student. The fields should be student_id, name, gender, date_of_birth, year_joined and GPA. Write get and set methods for each field. Also write an output method. Next create another class called CourseSection. Fields should be: Course_id, Semester, Array of students with their grades. Write methods to get and set the first fields, output all students, add a student, drop a student.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.