JavaScript Functions, Objects, Array, and Control Structures Dr. Charles Severance www.php-intro.com.

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Introduction to PHP Dr. Charles Severance
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
PHP Functions / Modularity
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Classes & Objects Computer Science I Last updated 9/30/10.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Syntax & terminology review While the following slides are not exactly what we did on the board (object diagrams are not shown here) they cover most of.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Introduction to Programming with Java, for Beginners Intro OOP with Java Java Program Structure.
OOP & JAVA. HelloWorld.java /** * The HelloWorld class is an application that * displays "Hello World!" to the standard output. */ public class HelloWorld.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
25-Jun-15 Starting Classes and Methods. Objects have behaviors In old style programming, you had: data, which was completely passive functions, which.
CS 106 Introduction to Computer Science I 03 / 19 / 2008 Instructor: Michael Eckmann.
Expressions and Control Flow in PHP Dr. Charles Severance
ASP.NET Programming with C# and SQL Server First Edition
Getting PHP Hosting Dr. Charles Severance. What We Need We want to be able to put up our application on the web so anyone can access our URL (and so we.
Introduction to Programming. To gain a sound knowledge of programming principles To gain a sound knowledge of object- orientation To be able to critically.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Forms and PHP Dr. Charles Severance
Object Oriented Software Development
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
The Document Object Model. Goals Understand how a JavaScript can communicate with the web page in which it “lives.” Understand how to use dot notation.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
PHP Arrays Dr. Charles Severance
Variables, Expressions, and Statements
Conditional Execution Chapter 3 Python for Informatics: Exploring Information
Object-Oriented Programming with Java Lecture 1: Introduction Autumn, 2007.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
PHP Functions Dr. Charles Severance
Using jQuery / APIs / JSON Dr. Charles Severance
Introduction to Object-Oriented Programming Lesson 2.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 27 I Love this Class.
JavaScript Dr. Charles Severance
Written by: Dr. JJ Shepherd
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Forms and PHP Dr. Charles Severance
PHP Arrays Dr. Charles Severance
PHP Functions / Modularity Dr. Charles Severance
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
PHP Functions Chapter 5 Dr. Charles Severance To be used in association with the book: PHP, MySql, and JavaScript by Robin Nixon.
Defining Classes. Why is Java designed this way? Improving our class Creating our own class Using our class Implementing our class.
What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Python Objects Charles Severance Python for Everybody
Our Technologies Dr. Charles Severance
Objects as a programming concept
PHP Functions / Modularity
Objects as a programming concept
Using JSON Dr. Charles Severance
PHP Object Oriented Programming (OOP)
Objects as a programming concept
Java Primer 1: Types, Classes and Operators
Loops and Iteration Chapter 5 Python for Everybody
HTTP Parameters and Arrays
Functions Chapter 4 Python for Everybody
Object Oriented Programming in JavaScript
Tuples Chapter 10 Python for Everybody
PHP Namespaces (in progress)
Your First Java Application
Charles Severance Single Table SQL.
Classes, Objects and Methods
Python Objects Charles Severance Python for Everybody
Model View Controller (MVC)
Python Objects Charles Severance Python for Everybody
Presentation transcript:

JavaScript Functions, Objects, Array, and Control Structures Dr. Charles Severance

Definitions Class - a template - Dog Method or Message - A defined capability of a class - bark() Object or Instance - A particular instance of a class - Lassie

Terminology: Class oriented_programming Defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes, fields or properties) and the thing's behaviors (the things it can do, or methods, operations or features). One might say that a class is a blueprint or factory that describes the nature of something. For example, the class Dog would consist of traits shared by all dogs, such as breed and fur color (characteristics), and the ability to bark and sit (behaviors).fieldspropertiesmethods

Terminology: Class oriented_programming A pattern (exemplar) of a class. The class of Dog defines all possible dogs by listing the characteristics and behaviors they can have; the object Lassie is one particular dog, with particular versions of the characteristics. A Dog has fur; Lassie has brown-and-white fur.

Terminology: Instance oriented_programming One can have an instance of a class or a particular object. The instance is the actual object created at runtime. In programmer jargon, the Lassie object is an instance of the Dog class. The set of values of the attributes of a particular object is called its state. The object consists of state and the behavior that's defined in the object's class.state Object and Instance are often used interchangeably.

Terminology: Method oriented_programming An object's abilities. In language, methods are verbs. Lassie, being a Dog, has the ability to bark. So bark() is one of Lassie's methods. She may have other methods as well, for example sit() or eat() or walk() or save_timmy(). Within the program, using a method usually affects only one particular object; all Dogs can bark, but you need only one particular dog to do the barking Method and Message are often used interchangeably.

Objects in JavaScript The OO pattern in JavaScript is a little different The function is indeed a store and reuse pattern The function keyword returns a value which is the function itself - it makes a function!

First-Class Functions In computer science, a programming language is said to have first-class functions if it treats functions as first- class citizens. Specifically, this means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures.

A Sample Class

function PartyAnimal() { this.x = 0; this.party = function () { this.x = this.x + 1; console.log("So far "+this.x); } an = new PartyAnimal(); an.party(); This is the template for making PartyAnimal objects. Each PartyAnimal object has a bit of data. Each PartyAnimal object has a bit of code. Create a PartyAnimal object. Tell the object to run the party() code. js01.ht m

function PartyAnimal() { this.x = 0; this.party = function () { this.x = this.x + 1; console.log("So far "+this.x); } an = new PartyAnimal(); an.party(); an an x: party( ) js01.ht m

Object Life Cycle

Object Life Cycle Objects are created, used and discarded Constructors are implicit in JavaScript - natural A constructor in a class is a special block of statements called when an object is created Destructors are not provided by JavaScript nce)

function PartyAnimal() { this.x = 0; console.log("In the 'constructor'"); this.party = function () { this.x = this.x + 1; console.log('So far '+this.x); } an = new PartyAnimal(); an.party(); js03.ht m

Many Instances We can create lots of objects - the class is the template for the object We can store each distinct object in its own variable We call this having multiple instances of the same class Each instance has its own copy of the instance variables

function PartyAnimal(nam) { this.x = 0; this.name = nam; console.log("Built "+nam); this.party = function () { this.x = this.x + 1; console.log(nam+"="+this.x); } s = new PartyAnimal("Sally"); s.party(); j = new PartyAnimal("Jim"); j.party(); s.party(); Constructors can have additional parameters. These can be used to setup instance variables for the particular instance of the class (i.e. for the particular obect). js04.ht m

function PartyAnimal(nam) { this.x = 0; this.name = nam; console.log("Built "+nam); this.party = function () { this.x = this.x + 1; console.log(nam+"="+this.x); } s = new PartyAnimal("Sally"); s.party(); j = new PartyAnimal("Jim"); j.party(); s.party(); s x: name: j x: name:

Definitions Class - a template - Dog Method or Message - A defined capability of a class - bark() Object or Instance - A particular instance of a class - Lassie Constructor - A method which is called when the instance / object is created

Arrays

JavaScript supports both linear arrays and associative structures, but the associative structures are actually objects

Linear Arrays

Array Constructor

Associative Arrays Objects JavaScript Associative Arrays are actually objects with member variables They can be accessed with either associative array syntax or object syntax

balls = {"golf": "Golf balls", "tennis": "Tennis balls", "ping": "Ping Pong balls"}; balls.soccer = "Soccer balls"; balls['lacross'] = "Lacross balls"; console.dir(balls); js05.ht m

balls = {"golf": "Golf balls", "tennis": "Tennis balls", "ping": "Ping Pong balls"}; balls.soccer = "Soccer balls"; balls.kick = function () { console.log('Boom!'); } console.dir(balls); balls.kick(); js06.ht m

Control Structures Lite...

Control Structures We use curly braces for control structure and whitespace / line ends do not matter If statements are as you would expect While loops are as you would expect Counted for loops are as you would expect In loops, break and continue are as you would expect

Definite Loops (for) balls = {"golf": "Golf balls", "tennis": "Tennis balls", "ping": "Ping Pong balls"}; for (ball in balls) { console.log(ball+' = '+balls[ball]); } js07.ht m

balls = {"golf": "Golf balls", "tennis": "Tennis balls", "ping": "Ping Pong balls"}; balls.kick = function () { console.log('Boom!'); } for (ball in balls) { console.log(ball+' = '+balls[ball]); }

balls = {"golf": "Golf balls", "tennis": "Tennis balls", "ping": "Ping Pong balls"}; balls.kick = function () { console.log('Boom!'); } for (ball in balls) { val = balls[ball]; if ( typeof val != "string" ) continue; console.log(ball+' = '+balls[ball]); } js09.ht m

Acknowledgements / Contributions These slides are Copyright Charles R. Severance ( as part of and made available under a Creative Commons Attribution 4.0 License. Please maintain this last slide in all copies of the document to comply with the attribution requirements of the license. If you make a change, feel free to add your name and organization to the list of contributors on this page as you republish the materials. Initial Development: Charles Severance, University of Michigan School of Information Insert new Contributors and Translators here including names and dates Continue new Contributors and Translators here