IS JAVASCRIPT OBJECT-ORIENTED? Contains objects Data Methods Doesn’t have classes Does have constructors Does have prototype functions Doesn’t provide.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
Fall 2007CSE 115/503 Introduction to Computer Science for Majors I1 Association relationship We’ve seen one implementation of “knows a”: public class Dog.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Reminder Check the course web site on a regular basis:
Road Map Introduction to object oriented programming. Classes
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Lecture 9 Concepts of Programming Languages
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Taking JavaScript Seriously IS NOT THE WORST IDEA.
Neal Stublen Key Concepts  Classes  Objects  Inheritance  Polymorphism  Encapsulation.
Chapter 4 Objects and Classes.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
JavaScript Design Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers SoftUni.
Local Variables Garbage collection. © 2006 Pearson EducationParameters2 of 10 Using Accessors and Mutators Together What if you want to get a property.
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
Agenda Object Oriented Programming Reading: Chapter 14.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
ALBERT WAVERING BOBBY SENG. Week 4: JavaScript  Quiz  Announcements/questions.
10-Nov-15 Java Object Oriented Programming What is it?
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Martin Kruliš by Martin Kruliš (v1.0)1.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
CSC 205 Java Programming II Defining & Implementing Classes.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
JavaScript Modules and Patterns Private Fields, Module, Revealing Module, Revealing Prototype, … Software University Technical Trainers.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Section 6.1 CS 106 Victor Norman IQ Unknown. The Big Q What do we get by being able to define a class?! Or Do we really need this?!
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Access Control to Class Members tMyn1 Access Control to Class Members In its support for encapsulation, the class provides two major benefits. First, it.
Prototype Chain and Inheritance Prototype chain, Inheritance, Accessing Base Members Software University Technical Trainers SoftUni Team.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Individual Testing, Big-O, C++ Bryce Boe 2013/07/16 CS24, Summer 2013 C.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Simulating OOP in JavaScript Function Constructor, Prototypes, "this" Object, Classical and Prototypal Model Software University Technical.
Getting from Scripts to Applications Donald J. Sipe | Refresh Jacksonville | November 11 th 2008.
Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI
Introduction to Javascript. What is javascript?  The most popular web scripting language in the world  Used to produce rich thin client web applications.
Bjarne Stroustrup I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my.
JavaScript Variables. Definition A variable is a "container" for information you want to store. A variable's value can change during the script.
JavaScript Patterns to Clean Up Your Code Dan Wahlin.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Andrew(amwallis) Classes!
OOP: Encapsulation &Abstraction
Classes (Part 1) Lecture 3
Phil Tayco Slide version 1.0 Created Sep 18, 2017
Dr. Charles W. Kann III JavaScript Objects Dr. Charles W. Kann III
Anatomy of a class Part I
Lecture 9 Concepts of Programming Languages
Creating Objects in a Few Simple Steps
Classes & Objects: Examples
Modern JavaScript Develop And Design
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Tonga Institute of Higher Education
Classes and Objects CGS3416 Spring 2019.
Encapsulation.
Anatomy of a class Part I
CSG2H3 Object Oriented Programming
Lecture 9 Concepts of Programming Languages
Presentation transcript:

IS JAVASCRIPT OBJECT-ORIENTED? Contains objects Data Methods Doesn’t have classes Does have constructors Does have prototype functions Doesn’t provide information hiding??? No private members “per se” Can closures be used???

DOUGLAS CROCKFORD’S METHOD // Person class – example of access to private data members via functions function Person(firstName, lastName) { // Define a `getName()` function for combining the private name variables. this.getName = function() { return firstName + " " + lastName; }; // Define a `setLastName()` function to update the private last name variable. this.setLastName = function(newLastName) { lastName = newLastName; return this; }; } // Sets age for object, but publicly Person.prototype.setAge = function(age) { this.age = age; }; // Getter for the public `age` property Person.prototype.getAge = function() { return this.age; };

DOWNFALL OF CROCKFORD’S METHOD All instances of the Person pseudo-class have unique private member accessor methods. var a = new Person("John", "Resig"), b = new Person("Douglas", "Crockford"); alert(a.getName == b.getName); // false alert(a.getAge == b.getAge); // true Prototype functions don’t have direct access to the private members. Person.prototype.getFirstName = function() { return this.getName().replace(/.*? /g, ""); }; No direct access to the private members. If first name has a space in it, this will not work correctly.

PRIVILEGED PROTOTYPE FUNCTIONS Prototype functions are good — no unnecessary duplication Prototype functions have access to any variables in their ancestral function scopes Let’s use a closure to solve our dilemma

PRIVILEGED PROTOTYPE FUNCTIONS (function(key) { // Define Person in global scope Person = function(firstName, lastName) { // Store private members in one object. var privateVars = { firstName : firstName, lastName : lastName }; // Define a getter function that will only return private members // for privileged functions. this._ = function(aKey) { return aKey === key && privateVars; }; /* PROTOTYPE FUNCTIONS DEFINED ON NEXT SLIDE */ })({});

PRIVILEGED PROTOTYPE FUNCTIONS (function(key) { /*...CODE IN CLOSURE FROM PREVIOUS SLIDE... */ // Define a getter for the full name. Person.prototype.getName = function() { var _ = this._(key); return _.firstName + " " + _.lastName; }; // Define a setter for the last name. Person.prototype.setLastName = function(newLastName) { this._(key).lastName = newLastName; return this; }; // Define a getter for the first name. Person.prototype.getFirstName = function() { return this._(key).firstName; }; })({});

PROS & CONS Pros Create prototype functions that now have access to private variables. Eliminate duplication of getters and setters for each instance. Using a closure to prevent access from outside code. Cons A function call to access private members. Still have one function that is being duplicated for each instance. Overwriting the `_` (underscore) function will render the object useless. If the closure code is changed, the private members can become public.

FINAL NOTES The pros outweigh the cons. The `_` (underscore) function is really lightweight. Developers rarely overwrite an object’s `_` (underscore) property. Being able to change the code will always be a con of client-side JS code. Currently in use jPaq Color object implementation of a JS Hashtable classHashtable JavaScript is not necessarily object-oriented, but due to its unique capabilities, it doesn’t need to be.