lesson15. javascript objects objects encapsulate data (properties) and behavior(methods); the...

19
Lesson15

Upload: dwain-brown

Post on 23-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

Lesson15

Page 2: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

JavaScript Objects

• Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

• A set of objects have been defined. Each object encapsulates the elements of an XHTML document and expose to a JavaScript programmer properties and methods that enables a JavaScript program to interact with the elements (objects) in an XHTML document

Page 3: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

Objects

• The browser's window object provides methods and properties that enable a script to manipulate a browser window

• When a string is assigned to the window object's status property, that string is displayed in the status bar of the browser window. The window object's alert method allows the programmer to display a message in a separate window.

Page 4: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

JavaScript

Different JavaScript/browser Objects are: • String Object

• Array Object

• Date Object

• Math Object

• Window Object • Frame Object

• Location object

• Document object

– Form Object

Page 5: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

JavaScript

document.write("<FONT COLOR='RED'>This is Red</FONT>")

• The document is know as an object

• The write is know as the object's method (an action to be preformed on the object)

• The double parentheses are called an instance. The text inside of the parentheses is called the method's parameters, or what will do done when the method is acted upon.

Page 6: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

JavaScript

• Methods act upon objects• Object properties: A property is like a subsection

of an object. It hold specific characteristics about the object.

• document.write("<BR>The URL of this page is <B>" +document.location+ "</B>.")

• The + instructs the program to go find the property's value and write it in the space.

• This script displays an example of an object property. Note: the capitalization pattern of each property. Every time you use these properties the pattern MUST be the same

Page 7: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

Functions

• The best way to develop and maintain a large program is to construct it from small modules.

• In JavaScript these modules are called Functions.

• JavaScript programs are written by combining new functions that programmers write with “prepackaged” functions and objects available in JavaScript.

• The “prepackaged” functions that belong to JavaScript objects are often called Methods. The term Method implies tht the function belongs to a particular object.

Page 8: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

Functions

• Programmers write programmer-defined functions to define specific tasks that may be used at many points in a script. The actual statements defining the function are written only once.

• A function is invoked by a function call. The function call specifies the function name and provides information (as arguments) that the called function needs to do its task.

• This allows programs to be modularized.

• All the variables declared in function definitions are local variables – they are known to the function in which they are defined.

Page 9: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

Functions

• Most functions have parameters.

• Parameters provide the means for communicating information between functions via function calls.

• Use functions as building blocks inorder to create new programs.

• Structured programming and software reusability is important.

Page 10: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

Functions

• The format of a function definition is

function function-name ( parameter-list )

{

declarations and statements

}

• Function-name any valid identifier.• Parameter-list list of parameters

Page 11: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

Functions

return; and return expression;

• Returns control to the point at which a function was invoked.

Examples:

• The Math object max method determines the larger of its two argument values.

Page 12: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

JavaScript

• With JavaScript, we do not need to specify a main function, like we do for C.

• You define functions at the beginning of a file (in the head section), and call them and call them anywhere on the page after they are defined.

• We use the keyword function , and then give the function a meaningful name.

Page 13: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

JavaScript

1 <!-- changecolour.html -->2 <html>3 <head>4 <title>Colour change</title>5 <script language=“JavaScript">6 <!-- hide JavaScript from old browsers7 function changeColour(color) {8 document.bgColor=color;9 }10 // stop hiding JavaScript -->11 </script>12 </head>

Page 14: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

Functions

• http://www.w3schools.com/js/js_functions.asp

Page 15: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

Functions

• A function with no arguments must include the parentheses:

function myfunction()

{

some statements

}

• By placing functions in the head section of the document, you make sure that all the code in the function has been loaded before the function is called

Page 16: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

Functions

How to Call a Function

• A function is not executed before it is called.

• You can call a function containing arguments:

myfunction(argument1,argument2,etc)

or without arguments:

myfunction()

Page 17: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

Function Example

function total(a,b){ result=a+b return result}

• When you call this function you must send two arguments with it:

sum=total(2,3)

Page 18: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

Review

A function is invoked with a ________________

function call

A variable known only within the function in which it is defined is called _________________

Local variable

Page 19: Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together

Review

The _____________ statement in a called function can be used to pass the value of an expression back to the calling function

return

a) Method g() {

document.writeln( “inside method g” );

}

b) function sum( x, y ) {

var result;

result = x + y

}

c) Function f( a ); {

document.writeln( a );

}

a) Method function b)return x + y c) remove ;