Object Oriented JavaScript. JavaScript Really only 4 types in JavaScript – number, string, boolean – Object (includes arrays) Remember that an object.

Slides:



Advertisements
Similar presentations
Web Application Development Slides Credit Umair Javed LUMS.
Advertisements

1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
1 Object-Oriented Programming Concepts. 2 Recap from last lecture Variables and types –int count Assignments –count = 55 Arithmetic expressions –result.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
These are the inheritance slides. They are slides just like all the other AP CS slides. But they are unique in that they all talk about inheritance.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Taking JavaScript Seriously IS NOT THE WORST IDEA.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Intro to OOP with Java, C. Thomas Wu
Chapter 3 Vector Class. Agenda Design and Implementation of Vector class – add, get, set remove, copy, equals, ensureCapacity Hangman using Vector class.
1 JavaScript. 2 What’s wrong with JavaScript? A very powerful language, yet –Often hated –Browser inconsistencies –Misunderstood –Developers find it painful.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Martin Kruliš by Martin Kruliš (v1.0)1.
Martin Kruliš by Martin Kruliš (v1.0)1.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
CSE 341 Lecture 26 OOP, prototypes, and inheritance slides created by Marty Stepp
Prototypal Inheritance. Can We Do Inheritance Without Classes? How do we share behaviour across objects.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Julian on JavaScript: Functions Julian M Bucknall, CTO.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CS1101 Group1 Discussion 6 Lek Hsiang Hui comp.nus.edu.sg
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
CS 112 Programming 2 Lecture 06 Inheritance & Polymorphism (1)
Object Oriented JavaScript. JavaScript Really only 4 types in JavaScript – number, string, boolean – Object (includes arrays) Remember that an object.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Class Fundamentals BCIS 3680 Enterprise Programming.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Introduction to Object-oriented Programming
Chapter 11 Inheritance and Polymorphism
Recitation 6 Inheritance.
Overloading and Constructors
Chapter 9 Inheritance and Polymorphism
Extending Classes.
Object-Oriented Programming in PHP
Tonga Institute of Higher Education
Chapter 8 Class Inheritance and Interfaces
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Presentation transcript:

Object Oriented JavaScript

JavaScript Really only 4 types in JavaScript – number, string, boolean – Object (includes arrays) Remember that an object is really just a bunch of key-value pairs Ways to create an object – Object literal – Create a new blank object – Write a constructor function

Constructor functions Normal JavaScript function Called with new like Java Generally sets some properties and methods using “this” By convention, constructors start with uppercase letters Called with new // Constructs and returns a new Point object. function Point(xValue, yValue) { this.x = xValue; this.y = yValue; this.distanceFromOrigin = function() { return Math.sqrt(this.x * this.x + this.y * this.y); }; }

Prototypes every object contains a reference to its prototype – default: Object.prototype ; strings → String.prototype ; etc. a prototype can have a prototype, and so on – an object "inherits" all methods/data from its prototype(s) – doesn't have to make a copy of them; saves memory – prototypes allow JavaScript to mimic classes, inheritance

Prototypes Every function/object stores a prototype object property in it – When we define the Point function (constructor) a Point.prototype is created – Every object created will use the function prototype object as its prototype When new is called – A new object is created  this – Attaches the function prototype to the object – Returns the new object

The Prototype Chain When you ask for a property or method, JavaScript – sees if the object itself contains the property – recursively checks the objects prototype – continues up the prototype chain until the end and returns undefined if it cannot find it. x: 3 y: -4 __proto__ Point.prototype distanceToOrigin __proto__ Object.prototype toString __proto__

Using the Prototype for Methods Adding a property/method to a prototype will make it available to all objects that use that prototype Any prototype can be modified – Including built-in types // adding a method to the prototype function.prototype.name = function(params) { statements; }; Point.prototype.distanceFromOrigin = function() { return Math.sqrt(this.x * this.x + this.y * this.y); };

Inheritance using the Prototype To make a subclass, set its prototype to an object of the superclass Question: Why not this way? – SubClassName.prototype = SuperClassName.prototype; function SuperClassName(parameters) {... } function SubClassName(parameters) {... } //Connect the prototype chain SubClassName.prototype = new SuperClassName(parameters); // Reset the constructor SubClassName.prototype.constructor = SubClassName

Inheritance // Constructor for Point3D "subclass" function Point3D(x, y, z) { this.x = x; this.y = y; this.z = z; } // set it to be a "subclass" of Point Point3D.prototype = new Point(0, 0); // override distanceFromOrigin method to be 3D Point3D.prototype.distanceFromOrigin = function() { return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z); };

Inheritance There is no equivalent of the super keyword – no easy way to call the superclass's constructor no built-in way to call an overridden superclass method – have to write it manually, e.g. var d = Point.prototype. distanceFromOrigin.apply(this); Or var d = Point.prototype. distanceFromOrigin.call(this); Apply requires an array for function args Call requires that function args are explicitly named

function A() // Define super class { this.x = 1; } A.prototype.DoIt = function() // Define Method { this.x += 1; } B.prototype = new A; // Define sub-class B.prototype.constructor = B; function B() { A.call(this); // Call super-class constructor (if desired) this.y = 2; } B.prototype.DoIt = function() // Define Method { A.prototype.DoIt.call(this); // Call super-class method (if desired) this.y += 1; } b = new B; document.write((b instanceof A) + ', ' + (b instanceof B) + ' '); b.DoIt(); document.write(b.x + ', ' + b.y);

Private Members Create private members using local variables and methods Closure allows access to the local variables function A() { var x = 7; this.GetX = function() { return x;} this.SetX = function(xT) { x = xT; } } obj = new A; obj2 = new A; document.write(obj.GetX() + ' ' + obj2.GetX()); obj.SetX(14); document.write(' ' + obj.GetX() + ' ' + obj2.GetX());

Namespaces In JavaScript, there are only two scopes – Local and global Problem: – In a large program, the namespace gets full – Using libraries complicates this more Solution – The JavaScript Module Pattern

The JavaScript Module Pattern Using global variables and self-executing functions, create the equivalent of a module Note: self-executing functions – Typically a function is just declared until called – Javascript allows us to declare the function and have it called immediately (function() { alert(“Hello World”); })(); OR (function() { alert(“Hello World”); }());

The JavaScript Module Pattern Global variable – cs340.module Everything else is private inside the module Protected namespace Returned object will run the constructor var cs340.module = (function () { // private variables and functions var foo = 'bar'; // constructor var module = function () { }; // prototype module.prototype = { constructor: module, something: function () { } }; // return the module return module; })(); var my_module = new cs340.module();

JavaScript Module Namespace catan.models.ClientModel = (function clientModelNameSpace(){ var ClientModel = (function ClientModelClass(){ var fullmodel = {}; function ClientModel(localConfig){ $("#debug").append(" In ClientModel Constructor"); } ClientModel.prototype.initFromServer = function(success){ … } ClientModel.prototype.getClientPlayerName = function(){ // TODO: Return the local player's correct name return "Sam"; } ClientModel.prototype.getClientPlayerColor = function(){ // TODO: Return the local player's correct color return "orange"; } return ClientModel; }()); return ClientModel; }()); var clientModel = new catan.models.ClientModel();