Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together."— Presentation transcript:

1 Lesson15

2 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

3 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.

4 JavaScript Different JavaScript/browser Objects are: String Object Array Object Date Object Math Object Window Object Frame Object Location object Document object –Form Object

5 JavaScript document.write(" This is Red ") 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.

6 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(" The URL of this page is " +document.location+ ".") 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

7 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.

8 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.

9 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.

10 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

11 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.

12 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.

13 JavaScript 1 2 3 4 Colour change 5 6<!-- hide JavaScript from old browsers 7function changeColour(color) { 8document.bgColor=color; 9} 10// stop hiding JavaScript --> 11 12

14 Functions http://www.w3schools.com/js/js_functions.asp

15 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

16 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()

17 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)

18 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

19 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 ;


Download ppt "Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together."

Similar presentations


Ads by Google