Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.

Slides:



Advertisements
Similar presentations
The Web Warrior Guide to Web Design Technologies
Advertisements

2440: 211 Interactive Web Programming JavaScript Fundamentals.
Advanced JS The World's Most Misunderstood Programming Language ) Douglas Crockford( Shimon Dahan
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
JavaScript, Fourth Edition
JavaScript, Third Edition
ASP.NET Programming with C# and SQL Server First Edition
XP Tutorial 1 New Perspectives on JavaScript, Comprehensive1 Introducing JavaScript Hiding Addresses from Spammers.
Week 3 Recap CSE 115 – Fall Java Source Code File Made up of: Package declaration Class definition.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
Introduction to scripting
1 CS101 Introduction to Computing Lecture 29 Functions & Variable Scope (Web Development Lecture 10)
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
Introduction to JavaScript 11 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 14.
JavaScript – Part II Data Types and Operations George Mason University June 3, 2010.
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.
Database-Driven Web Sites, Second Edition1 Chapter 3 INTRODUCTION TO CLIENT-SIDE SCRIPTS.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
JavaScript, Fourth Edition
Introduction to JavaScript Gordon Tian
1 JavaScript in Context. Server-Side Programming.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
JavaScript Shopping Cart Yong Choi CSU Bakersfield.
WDMD 170 – UW Stevens Point 1 WDMD 170 Internet Languages eLesson: Variables, Functions and Events (there is an audio component to this eLesson) © Dr.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
CS346 Javascript -3 Module 3 JavaScript Variables.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Custom JavaScript Objects JavaScript 7. Constructor Functions Custom JavaScript objects are based on functions called constructor functions Constructor.
Chapter 2: Variables, Functions, Objects, and Events JavaScript - Introductory.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
1 JavaScript in Context. Server-Side Programming.
JavaScript 1 COE 201- Computer Proficiency. Introduction JavaScript scripting language ▫Originally created by Netscape ▫Facilitates disciplined approach.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
MTA EXAM HTML5 Application Development Fundamentals.
MIT-AITI: Functions Defining and Invoking Functions Functions as Data Function Scope: The call Object Function Arguments: The arguments objects Function.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
JavaScript Variables. Definition A variable is a "container" for information you want to store. A variable's value can change during the script.
Chapter 6 JavaScript: Introduction to Scripting
Functions Comp 205 Fall ‘17.
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
University of Central Florida COP 3330 Object Oriented Programming
JavaScript Syntax and Semantics
4. Javascript Pemrograman Web I Program Studi Teknik Informatika
Chapter 4 Procedural Methods.
METHODS AND BEHAVIORS AKEEL AHMED.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Unit-1 Introduction to Java
Chapter 7 Procedural Methods.
Object Oriented Programming in java
JavaScript: Introduction to Scripting
Use a variable to store a value that you want to use at a later time
Presentation transcript:

Tutorial 2 Variables and Objects

Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can vary over time –Cannot use reserved words as variables Reserved Words or Keywords are part of the JavaScript language syntax –Variable Example: employeeName

Working with Variables and Objects Variables –To create: Use keyword var to declare the variable Use the assignment operator to assign the variable a value –Declare, then assign value (initialize) var employeeName; employeeName = “Ric”; –Declare and assign variable in a single statement var employeeName = “Ric”; –Once created: May be changed at any point in the program –Use variable name and assignment operator employeeName = “Althea”;

Working with Variables and Objects Variables –Syntax rules Cannot use: –Reserved words & spaces Must begin with one of the following: –Uppercase or lowercase ASCII letter –Dollar sign ($) or underscore (_) Can use numbers, but not as first character Variables are case-sensitive

Working with Variables and Objects Variables –Conventions Use underscore or capitalization to separate words of an identifier –employee_first_name –employeeFirstName

Variables Can write the value contained in a variable to a web page: var myName = “john”; document.writeln(“Hello “); document.writeln(myName); Can use the + concatenation operator: var myName = “john”; document.writeln(“Hello “ + myName);

Working with Variables and Objects Variable Scope –Defines where in the program a declared variable can be used Global variable –Declared outside a function and is available to all parts of the program –var keyword optional Local variable –Declared inside a function and is only available within the function it is declared Global and local variables can use same identifier

Working with Variables and Objects function putStuff(myName, myNum){ document.writeln(“Hello “ + myName + “, how are you?”); var rslt = myNum * 2; document.writeln (myNum + “ * 2 = “, rslt); } putStuff(“John”, 5);

Working with Variables and Objects Objects are similar to built-in programs that contain their own functions and variables. You access a function or variable that belongs to an object using the dot syntax: document.writeln(``my message’’);

Objects There are built-in objects that relate to the browser and its contents: –Document –Window –Navigator –Screen –Element –Event –Form

Working with Variables and Objects There are also built-in JavaScript objects that extend the functionality of the language: JavaScript includes 11 built-in objects that relate to the language itself Each object contains various methods and properties for performing a particular task Can be used directly in program without instantiating a new object

Working with Variables and Objects Understanding JavaScript Objects –In OO languages (Java, C++) Class structures contain associated variables, methods (functions) and statements Objects are instances of classes –(i.e., objects are instantiated from classes) Classes can be inherited from other classes –JavaScript is not truly object-oriented Cannot create classes

Working with Variables and Objects Understanding JavaScript Objects –Custom JavaScript objects Based on constructor functions –Instantiate a new object or extending an old object –Objects inherit all variables and statements of constructor function Any JavaScript function can serve as a constructor

Working with Variables and Objects Understanding JavaScript Objects –Constructor function Has two types of elements –Property (field) »Variable within a constructor function »Data of the object –Method »Function called from within an object »Can be custom or built-in function

Working with Variables and Objects Understanding JavaScript Objects –Constructor function Identifier naming convention –First word uppercase to differentiate from non- constructor functions »BankEmployee The this keyword –Used in constructor functions to refer to the current object that called the constructor function

Working with Variables and Objects Understanding JavaScript Objects –Constructor function The new keyword –Used to create new objects from constructor functions –Example: »Achmed = new BankEmployee(name, empNum);

Working with Variables and Objects Understanding JavaScript Objects –Custom object inheritance and prototypes Objects inherit the properties and methods of their constructor function Constructor functions: –Do not require parameters –Do not require passed arguments »Properties may set the value at a later time »If used before set, will return an undefined value

Working with Variables and Objects Understanding JavaScript Objects –Custom object inheritance and prototypes Adding properties after object is instantiated –Properties can be added to objects using dot operator (.) –These properties available only to that specific object Prototype properties –Properties added using prototype keyword –Available to all objects that extend the constructor function »BankEmployee.prototype.department = “”;

Working with Variables and Objects Understanding JavaScript Objects –Custom object inheritance and prototypes Object definitions can extend other object definitions –Extend original definition and add new properties or function calls

Working with Variables and Objects Understanding JavaScript Objects –Custom object methods Functions associated with a particular object –Define the function –Use the this reference to refer to object properties –Add it to the constructor function »this.methodName = functionName Call the method using the dot operator (.)