Docstoc

javascript language

Document Sample
javascript language Powered By Docstoc
					The JavaScript Language




                     13
The JavaScript Language

n Syntaxs
n Variables
n Data Types
n Statements
n Object-Oriented JavaScript




                               14
Syntaxs

n   Case sensitivity
    ¨ JavaScript   is a case-sensitive language.


n   Whitespace
    ¨ JavaScript ignores spaces, tabs, and
      newlines that appear between tokens in
      programs.


                                                   15
Syntaxs
n   Semicolons
    ¨ Simple statements in JavaScript are generally
      followed by semicolons (;)
     a = 3;
     b = 4;

n   Comments
     // This is a single-line comment.
     /* This is also a comment */ // and here is another comment.
     /*
      * This is yet another comment.
      * It has multiple lines.
      */                                                            16
Syntaxs
n   Identifiers         Reserved Words
     i
     my_variable_name
     v13
     _dummy
     $str




                                         17
Data Types
n   Numbers
    ¨ Numbers   in JavaScript are represented in 64-bit
     floating-point format.
      3.14
      2345.789
      .333333333333333333
      6.02e23             // 6.02 x 1023
    ¨ The toString( ) method converts an integer to a string,
     using the radix, or base, specified by its argument
     (the base must be between 2 and 36).
      var x = 33;
      var y = x.toString(2);   // y is "100001"
                                                            18
Data Types
n   Booleans
    ¨ Theboolean type has two possible values,
     represented by the JavaScript keywords true
     and false.
    ¨ Booleanvalues are typically used in
     JavaScript control structures.
       if (a == 4)

        b = b + 1;

       else

        a = a + 1;                                 19
Data Types
n   Strings
    ¨A   JavaScript string is a sequence of arbitrary
      letters, digits, and other characters from the
      16-bit Unicode character set.
         ""      //The empty string: it has zero characters
         'testing'      //Use Single Quote
         "3.14"         //Use Double Quote
         'name="myform"'
         "Wouldn't you prefer O'Reilly's book?"

                                                              20
Data Types
n   Objects
    ¨ An object is a compound data type that
      contains any number of properties. Each
      property has a name and a value.
           var o = new Object( );

           o.x = 1;
           o.y = 2;
           o.total = o.x + o.y;


                                                21
Data Types
n   Arrays
    ¨ Anarray is a type of object that contains
      numbered values rather than named values.

       a[0] = 1;
       a[1] = a[0] + a[0];
       var a = new Array( ); // Empty array
       var b = new Array(10); // 10 elements
       var c = new Array(1,2,3); // Elements 1,2,3


                                                     22
Data Types
n   Functions and methods
    ¨A  function is a piece of JavaScript code that is
     defined once and can be executed multiple
     times by a program.
             function sum(x, y) {
                     return x + y;
             }


             var total = sum(1,2);   // Total is now 3

                                                         23
Data Types
n   null and undefined
    ¨ The  JavaScript keyword null is a special value
      that indicates "no value".
    ¨ In JavaScript 1.5, there is a pre-defined global
      variable named undefined that holds this
      special undefined value.




                                                    24
Variables

n   Variable Typing:
    ¨ JavaScript    is untyped.
     i = 10;
     i = "ten";

n   Variables are declared and initialized with
    the var statement:
              var i = 1+2+3;
              var x = 3, message = 'hello world';
              for(var i = 0; i < 10; i++)
                      document.write(i + "<br>");   25
Variables

n   Variable scope
    ¨A  global variable has global scope; it is
      defined everywhere in your JavaScript code.
    ¨ A local variables declared within a function
      and have local scope.
      var scope = "global";      // Declare a global variable
      function checkscope( ) {
        var scope = "local";   // Declare a local variable with the same name
        document.write(scope); // Use the local variable, not the global one
      }
      checkscope();

      Output: local                                                        26
Variables

n   Primitive Type
     var a = 3.14; // Declare and initialize a variable
     var b = a; // Copy the variable's value to a new variable
     a = 4;     // Modify the value of the original variable
     alert(b)    // Displays 3.14; the copy has not changed


n   Reference Type
     var a = [1,2,3]; // Initialize a variable to refer to an array
     var b = a;      // Copy that reference into a new variable
     a[0] = 99;      // Modify the array using the original reference
     alert(b);     // Display the changed array [99,2,3] using the new reference

                                                                             27
Statements
n   Expression statements
    ¨ Every  JavaScript expression can stand alone
      as a statement. Assignments, method calls,
      increments, and decrements are expression
      statements.

        s = "hello world";
        x = Math.sqrt(4);
        x++;


                                                 28
Statements
n   Compound Statements
               also has a way to combine a
    ¨ JavaScript
     number of statements into a single statement,
     or statement block.

            {
                   x = Math.PI;
                   cx = Math.cos(x);
                   alert("cos(" + x + ") = " + cx);
            }


                                                      29
Statements
n   if
     ¨ The    if statement is the fundamental control
         statement that allows JavaScript to make
         decisions, or, more precisely, to execute
         statements conditionally.
             if (expression)     if (expression)
                     statement           statement1
                                 else
                                         statement2

                                                        30
Statements
n   else if
     if (n == 1) {
                  // Execute code block #1
     }
     else if (n == 2) {
                   // Execute code block #2
     }
     else {
               // If all else fails, execute block #3
     }



                                                        31
Statements
n   switch
    ¨ The  switch statement handles exactly this
      situation, and it does so more efficiently than
      repeated if statements.
         switch (expression) {
              statements
         }




                                                        32
Statements
n   while
    ¨ The  while statement is the basic statement
      that allows JavaScript to perform repetitive
      actions.
       while (expression)
         statement

       var count = 0;
       while (count < 10) {
         document.write(count + "<br>");
         count++;
       }
                                                     33
Statements
n   do/while
    ¨ The   do/while loop is much like a while loop,
       except that the loop expression is tested at
       the bottom of the loop rather than at the top.
     do                    function printArray(a) {
       statement              if (a.length == 0)
     while (expression);          document.write("Empty Array");
                             else {
                                  var i = 0;
                                  do {
                                    document.write(a[i] + "<br>");
                                  } while (++i < a.length);
                             }
                           }                                         34
Statements
n   for
    ¨ The for statement provides a looping
      construct that is often more convenient than
      the while statement.
          for(initialize ; test ; increment)
             statement

          for(var count = 0 ; count < 10 ; count++)
             document.write(count + "<br>");




                                                      35
Statements
n   for/in
     ¨ The  for/in statement provides a way to loop
        through the properties of an object
for (variable in object)   var person = new Object();
   statement               person.name="tom";
                           person.age=22;
                           for(prop in person){
                              document.write(prop +": "+ person[prop]+"<br/>");
                           }

                           Output:
                                     name: tom
                                     age: 22
                                                                            36
Statements
n   try/catch/finally
    ¨ The try/catch/finally statement is JavaScript's
      exception-handling mechanism.
      function message(){
               try{
                        addalert("Welcome guest!");
               }
               catch(err){
                        var txt="Error: " + err.description + "\n\n";
                        alert(txt);
               }
      }
      message();

      Alert: Error: undefined                                           37
Object-Oriented JavaScript

n   JavaScript provides a simple inheritance
    mechanism, and it is possible to define
    new classes of objects for use in your own
    programs.
      function Point(x,y) { // Constructor for Point
              this.x = x;         // Initialize X coordinate
              this.y = y;         // Initialize Y coordinate
      }
      var p =new Point(10,20);
      document.write(p.x);

      Output: 10
                                                               38

				
DOCUMENT INFO
Shared By:
Stats:
views:9
posted:5/26/2012
language:
pages:26
Description: javascript language