JavaScript Objects


Javascript objects

A class is the blueprint that describes properties and methods of an object.

An object is an instance of the class.


Javascript object components

A constructor initializes the object with appropriate values.

A field is a variable that holds data.

A property is also known as a getter or setter and accesses the fields. It is a method.

A method defines a behaviour that the object can perform.


Create a class


function Instructor( fname, lname, noOfCourse, dept ) {
this.firstname = fname;
this.lastname = lname;
this.noOfCourse = noOfCourse;
this.dept = dept;
this.getFullName = function() {
  return this.firstname + " " + this.lastname;
}
}

Create an object


var hamdy = new Instructor( "Hamdy", "Ibrahim", "5", "ITC" );

Built in objects

  • forms[] is an Array of form objects
  • links[] is an Array of hyperlink (a) objects
  • URL is the property for the current page
  • clear() is a method for clearing the window
  • write()is a method for sending output to the browser for display

Image Object Instantiation


photo1 = new Image();
photo1.src = "photo1.jpg";

photo2 = new image();
photo2.src = "photo2.gif";

for(i = 0; i < 5; i++) {
photo[i] = new image();
photo[i].src = "photo" + i + ".gif";
}

Regular Expression Object

Used to describe a Pattern of CHaracters

Useful in Performing Pattern-Matching, Search, & Replace

Basic Syntax: /Pattern/Modifiers or Attributes

Pattern: characters you would like to search/replace

Modifiers: (i) Case-Insensitive, (g) Global Matching, & (m) Multiline Matching

Brackets [] can be used in Pattern to identify Range of CHaracters


var myRegExp = /web/ig;
result = myRegExp.exec( "Welcome to Web Development" );
}