1 / 62

CHAPTER 3 FUNCTIONS, METHODS & OBJECTS

CHAPTER 3 FUNCTIONS, METHODS & OBJECTS. WHAT IS A FUNCTION?. Functions let you group a series of statements together to perform a specific task. Functions are reusable and save you from writing out the same code over and over. DECLARING A FUNCTION. function. function sayHello().

Télécharger la présentation

CHAPTER 3 FUNCTIONS, METHODS & OBJECTS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 3FUNCTIONS, METHODS & OBJECTS

  2. WHAT IS A FUNCTION?

  3. Functions let you group a series of statements together to perform a specific task.

  4. Functions are reusable and save you from writing out the same code over and over.

  5. DECLARING A FUNCTION

  6. function

  7. function sayHello()

  8. function sayHello() { document.write(‘Hello’);}

  9. KEYWORD function sayHello() { document.write(‘Hello’);}

  10. FUNCTION NAME function sayHello() { document.write(‘Hello’);}

  11. function sayHello() { document.write(‘Hello’);} CODE BLOCK (IN CURLY BRACES)

  12. CALLING A FUNCTION

  13. sayHello();

  14. FUNCTION NAME sayHello();

  15. DECLARING A FUNCTION THAT NEEDS INFORMATION

  16. function

  17. function getArea()

  18. function getArea(width, height)

  19. function getArea(width, height) { return width * height;}

  20. PARAMETER PARAMETER function getArea(width, height) { return width * height;}

  21. function getArea(width, height) { return width * height;} THE PARAMETERS ARE USED LIKE VARIABLES WITHIN THE FUNCTION

  22. CALLING A FUNCTION THAT NEEDS INFORMATION

  23. getArea(3, 5);

  24. getArea(3, 5); ARGUMENTS

  25. OBJECTS

  26. Objects group together variables and functions to create a model.

  27. In an object, variables and functions take on new names. They become properties and methods.

  28. Each property or method consists of a name (also known as a key) and its corresponding value.

  29. var hotel = { name: ‘Quay’, rooms: 40, booked: 25, gym: true, roomTypes: [‘twin’, ‘double’, ‘suite’], checkAvailability: function() { return this.rooms - this.booked; } };

  30. var hotel = { name: ‘Quay’, rooms: 40, booked: 25, gym: true, roomTypes: [‘twin’, ‘double’, ‘suite’], checkAvailability: function() { return this.rooms - this.booked; } };

  31. NAMES (KEYS) var hotel = { name: ‘Quay’, rooms: 40, booked: 25, gym: true, roomTypes: [‘twin’, ‘double’, ‘suite’], checkAvailability: function() { return this.rooms - this.booked; } };

  32. VALUES var hotel = { name: ‘Quay’, rooms: 40, booked: 25, gym: true, roomTypes: [‘twin’, ‘double’, ‘suite’], checkAvailability: function() { return this.rooms - this.booked; } };

  33. PROPERTIES var hotel = { name: ‘Quay’, rooms: 40, booked: 25, gym: true, roomTypes: [‘twin’, ‘double’, ‘suite’], checkAvailability: function() { return this.rooms - this.booked; } };

  34. METHOD var hotel = { name: ‘Quay’, rooms: 40, booked: 25, gym: true, roomTypes: [‘twin’, ‘double’, ‘suite’], checkAvailability: function() { return this.rooms - this.booked; } };

  35. ACCESSING OBJECTS

  36. var hotelName = hotel.name;

  37. var hotelName = hotel.name; OBJECT

  38. var hotelName = hotel.name; PROPERTY

  39. var hotelName = hotel[‘name’];

  40. var hotelName = hotel[‘name’]; OBJECT

  41. var hotelName = hotel[‘name’]; PROPERTY

  42. UPDATING OBJECTS

  43. hotel.name = ‘Park’;

  44. hotel.name = ‘Park’; OBJECT

  45. hotel.name = ‘Park’; PROPERTY

  46. hotel.name = ‘Park’; NEW VALUE

  47. hotel[‘name’] = ‘Park’;

  48. hotel[‘name’] = ‘Park’; OBJECT

  49. hotel[‘name’] = ‘Park’; PROPERTY

More Related