Web Design and Development Lecture 9 B - JavaScript Advanced
Web Design and Development Lecture 9 B - JavaScript Advanced
development
Lecture 9
Course Instructor: Wardah Mahmood
Email Id: wardah.mahmood@riphah.edu.pk
JavaScript Objects
JavaScript Objects
• In JavaScript, almost "everything" is an object.
• Booleans can be objects (if defined with the new keyword)
• Numbers can be objects (if defined with the new keyword)
• Strings can be objects (if defined with the new keyword)
• Dates are always objects
• Maths are always objects
• Regular expressions are always objects
• Arrays are always objects
• Functions are always objects
• Objects are always objects
• All JavaScript values, except primitives, are objects.
JavaScript Objects
• There are two basic ways to create an empty object:
Using constructor
and, the second is called object literal syntax, and is more convenient. This syntax is also the
core of JSON format and should be preferred at all times.
See: http://www.w3schools.com/js/js_objects.asp
JavaScript Objects (Using
Constructor)
function person(first, last, age) {
this.firstName = first;
this.lastName = last;
this.age = age;
}
p.firstName="Jane";
p.lastName="Doe";
p.age=30;
JavaScript Objects (Using Literal)
var person = {
firstName:"",
lastName:"",
age:0
};
p.firstName="Jane";
p.lastName="Doe";
p.age=30;
JavaScript Objects are Mutable
Objects are mutable: They are addressed by reference, not by value.
var x = person;
this.fullName = function () {
return this.firstName + " " + this.lastName;
}
}
var fn=p.fullName();
JavaScript Object Prototypes
• Every JavaScript object has a prototype.
• All JavaScript objects inherit the properties and methods from their prototype.
• Objects created using an object literal, or with new Object(), inherit from a prototype called
Object.prototype.
• The standard way to create an object prototype is to use an object constructor
function:
function person(first, last, age) {
this.firstName = first;
this.lastName = last;
this.age = age;
}
var p = new person("John", "Doe", 40);
Extending Instance
function person(first, last, age) {
this.firstName = first;
this.lastName = last;
this.age = age;
}
p.gender = "Male";
p.fullName = function() {
return this.firstName + " " + this.lastName;
};
Extending Prototype
function person(first, last, age) {
this.firstName = first;
this.lastName = last;
this.age = age;
}
person.prototype.gender = "Male";
person.prototype.fullName = function() {
return this.firstName + " " + this.lastName;
};
Inheritance Using Prototype
function parentClass() {
this.parentProperty1 = "A";
this.parentMethod1 = function(arg1) {
return "Parent Method 1: " + arg1; };
}
function childClass() {
this.childProperty1 = "B";
this.childMethod1 = function(arg1) {
return "Child Method 1: " + arg1; };
}
childClass.prototype=new parentClass();
function childClass() {
this.childProperty1 = "B";
this.childMethod1 = function(arg1) {
return "Child Method 1: " + arg1; };
}
childClass.prototype=new parentClass();
childClass.prototype.parentMethod1 = function(arg1) {
return "Overloaded Parent Method 1: " + arg1; };
See: http://www.w3schools.com/jsref/jsref_obj_string.asp
JavaScript String Object
var txt = "Hello World!";
See: Example 05
JavaScript String Object
Escape sequences behave as in Java
For Example:
See: Example 07
JavaScript Array Object
Three ways to initialize an array in JavaScript:
function example() {
for (var i = 0; i < arguments.length; i++)
{
alert(arguments[i]);
}
}
example("how", "are", "you");
See: Example 01
Arrays as Maps
• The index of a JS array need not be integers!
• This allows you to store mappings between an index of any type ("keys") and value
• Similar to Java's Map collection or a hash table data structure
See: Example 02
The For...In Loop
Loops over every index of the array, or every property name of the object
See: Example 03
JavaScript Date Object
Four ways of instantiating a date:
// date string
var d2 = new Date("October 17, 1979 11:13:00");
var x = Math.PI;
var y = Math.sqrt(16);
• Note: Math is not a constructor. All properties and methods of Math can be
called by using Math as an object without creating it.
JavaScript Math Object
Methods:
• abs(x) Returns the absolute value of x
• ceil(x) Returns x, rounded upwards to the nearest
• floor(x) Returns x, rounded downwards to the nearest integer
• max(x,y,z,...,n) Returns the number with the highest value
• min(x,y,z,...,n) Returns the number with the lowest value
• random() Returns a random number between 0 and 1
• round(x) Rounds x to the nearest integer
See: http://www.w3schools.com/jsref/jsref_obj_math.asp
JavaScript Math Object
x=Math.random();
x=Math.floor(x*11);
See: Example 06
JavaScript RegExp Object
• RegExp, is short for regular expression.
• A regular expression is an object that describes a pattern of characters.
• Regular expressions are used to perform powerful pattern-matching and "search-and-replace"
functions on text.
var patt=new RegExp(pattern,modifiers);
or more simply:
var patt=/pattern/modifiers;
• Pattern specifies the pattern of an expression
• Modifiers specify if a search should be global, case-sensitive, etc.
See: http://www.w3schools.com/js/js_obj_regexp.asp
JavaScript RegExp Object
Example 1
Do a case-insensitive search for "w3schools" in a string:
document.write(str.replace(/microsoft/gi, "W3Schools"));
JavaScript RegExp Object
RegExp test() Method
The test() method searches a string for a specified value, and returns true or false,
depending on the result. The following example searches a string for the character "e":
function validateEmail(elementValue)
{
var emailPattern;
emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return emailPattern.test(elementValue);
}
if (validateEmail("user@domain.com"))
{ document.write("Valid Email"); }
else
{ document.write("Invalid Email"); }