JavaScript/Functions and Objects 2

      Clipboard

      To do:
      The page should be deleted. The page JavaScript/Functions and Objects has higher coverage, and now that I've copied the section on objects from here over there, should be in all regards superior to this one. --Dan Polansky (talk) 22:12, 7 September 2008 (UTC)

      Functions

      A Javascript function is a code-block that can be reused. It can be called via an event, or by manual calling.

      Basic Example

      function myFunction(string) {
              alert(string);
              document.innerHTML += string;
      }
      myFunction("hello");
      

      The example would first:

      • Define the myFunction function
      • Call the myFunction function with arguement "hello"

      The result:

      • An alert message with 'hello'
      • The string 'hello' being added to the end of the document's/page's HTML.
      ↑Jump back a section

      Objects

      The great thing about Javascript is that it is an OOP (Object Oriented Programming) language. This means that with an OOP language, you can make your own objects (like the document object) and even define your own variable types.

      Basic Example

      var site = new Object(); //Required to not cause error in Internet Explorer
      site = {};
      site.test = function(string) {
              alert("Hello World! "+string);
              site.string = string;
      }
      site.test("Boo!");
      alert(site.string);
      

      The example would first:

      • Define site as an object
      • Define site as a blank object
      • Define site.test function
      • Call the site.test function with variable "Boo!"

      The result:

      • An alert message with 'Hello World! Boo!'
      • site.string being defined as string
      • An alert message with 'Boo!'.
      ↑Jump back a section
      Last modified on 10 September 2008, at 11:51