|   | Emerging Technology Fall Semester, 2004 JavaScript part 2 | |
|---|---|---|
| © 2004, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 08-Sep-04 | 
CS 683 Emerging Technologies Fall Semester, 2004 Doc 5 JavaScript part 2
Copyright ©, All rights reserved. 2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent ( http://www.opencontent.org/opl.shtml) license defines the copyright on this document.
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 2 | 
JavaScript: The Definitive Guide, 4th Ed., David Flanagan, O’Reilly, 2002
ECMAScript Language Specification v3, Dec 1999,
http://www.ecma-international.org/publications/files/ecma-st/ECMA-262.pdf
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 3 | 
var a = 'global';
function test() 
   {
   print( a);
   }
   
function perform(aFunction )
   {
   var a = 'local';
   aFunction();
   }
   
perform(test);
Code prints global
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 4 | 
var b = 'global';
   
function give( )
   {
   var b = 'local';
   function inner() 
      {
      print( b );
      }
   return inner;
   }
   
var test = give();
test();
Code prints local
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 5 | 
try
   {
   var n = prompt( “Enter positive integer” , “”);
   var result = factorial(n);
   alert(‘Answer is: ‘ + result);
   }
catch (exception )
   {
   alert(exception);
   }
finally
   {
   alert( ‘Done’);
   }
throw new Error(“a foobar occurred”); throw 5; throw ‘Cat’;
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 6 | 
var anObject = new Object();
   
var now = new Date();      //now
   
var semesterStart = new Date(2004, 8, 30);
   
var circle = { x: 0, y:0, radius: 3 }
   
print( circle.x);
print( circle.y);
print( circle.radius);
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 7 | 
function Circle(x, y, radius)
   {
   this.x = x;
   this.y = y;
   this.radius = radius;
   }
   
var y = new Circle(1, 2, 3);
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 8 | 
var circle = { x: 0, y:0, radius: 3 };
x, y, radius are properties of the object circle
circle.x = 12;         //set
   
var z = circle.y;      // read
   
circle.theta = 1.342;   // create new property
   
var w = {z: circle, name: ‘sam’ };
   
w.z.radius;         // nested properties
   
var a = circle[‘x’];       //read x
   
circle[‘x’] = 55;         //set x
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 9 | 
var circle = { x: 0, y:0, radius: 3 }
   
for (var name in circle)
   print( name);
x y radius
for (var name in circle) print( circle[name]);
0 0 3
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 10 | 
function Circle(x, y, radius)
   {
   this.x = x;
   this.y = y;
   this.radius = radius;
   }
   
function computeArea()
   {
   return Math.PI * this.radius * this.radius;
   }
   
var circle = new Circle(0, 0, 3);
   
circle.area = computeArea;
   
circle.area();         //28.274
var x = new Circle(0, 0, 3); x.area() //raises an exception
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 11 | 
function computeArea()
   {
   return Math.PI * this.radius * this.radius;
   }
   
function Circle(x, y, radius)
   {
   this.x = x;
   this.y = y;
   this.radius = radius;
   this.area = computeArea;
   }
   
var x = new Circle(0, 0, 3);
   
x.area()            //28.274
Now every circle object will contain an area property. All of them will contain the same value. This is a waste of space
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 12 | 
function computeArea()
   {
   return Math.PI * this.radius * this.radius;
   }
   
function Circle(x, y, radius)
   {
   this.x = x;
   this.y = y;
   this.radius = radius;
   }
   
Circle.prototype.area = computeArea;
Circle.prototype.pi = 3.1415;
   
var x = new Circle(0, 1, 3);
   
x.area();
x.pi;
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 13 | 
Each object has a prototype
Constructors define new types of objects
An object’s prototype starts as a clone of Object’s prototype
circle.x;
If the object has the property return its value
If not check the object’s prototype for the property and return its value
If don’t find the property it is not defined
circle.x = 23;
If the object has the property set the value
If not create the property in the object and set it.
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 14 | 
function increase(n) {return n +1;};
   
function Circle(x, y, radius)
   {
   this.x = x;
   this.y = y;
   this.radius = radius;
   }
   
Circle.PI = 3.1425;
   
Circle.increase = increase;
   
   
Circle.PI;               //returns 3.1425
   
Circle.increase(3 );      //returns 4
   
var x = new Circle(1, 2, 3);
   
x.PI;               //not defined
   
x.increase(3);            //Exception thrown
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 15 | 
function Rectangle(centerX, centerY, height, width)
   {
   this.x = centerX;
   this.y = centerY;
   this.height = height;
   this.width = width;
   }
   
Rectangle.prototype.area = function()
      {return this.height * this.width; }
   
Rectangle.prototype.isSquare = function()  {return false; }
   
function Square(centerX, centerY, height)
   {
   this.x = centerX;
   this.y = centerY;
   this.height = height;
   this.width = height;
   }
   
Square.prototype = new Rectangle(0,0,0,0);
Square.prototype.constructor = Square;
   
Square.prototype.isSquare = function()   {return true; }
   
var x = new Square(0,0,2);
x.area();
| CS 683 Fall 04 | Doc 5, JavaScript part 2 Slide # 16 | 
toString()
toLocaleString()
valueOf()
Copyright ©, All rights reserved.
2004 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA.
OpenContent license defines the copyright on this document.