CSS Chapter 1
CSS Chapter 1
Programming
Unit - I
• Website scripts run in one of two places – the client side, also called
the front-end, or the server side, also called the back-end.
• The client of a website refers to the web browser that is viewing it. The
server of a website is, of course, the server that hosts it.
• This language is designed primarily for building web pages. Java script
can be used along with HTML to create better web pages.
oInternal built-in objects (e.g. Window object: this object can be used to
manipulate the way current output window/frame in the browser displays its
content )
• Java script can run on both client side (by the browser after
downloading the page) and server side (with the help of Livewire
environment)
• (Digits are not allowed as the first character so that JavaScript can
easily distinguish identifiers from numbers.)
oi
omy_variable_name
ov13
o_dummy
o$str
• If a variable is declared but not initialized then its initial value is undefined.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
17
MHSSP
Variables in JavaScript
• The scope of a variable is the region of your program
source code in which it is defined.
• These initializer expressions are sometimes called “object literals” and “array
literals.”
• Unlike true literals, however, they are not primary expressions, because they
include a number of sub expressions that specify property and element values.
• The value of an array initializer is a newly created array. The elements of this new
array are initialized to the values of the comma-separated expressions:
• Object initializer expressions are like array initializer expressions, but the square
brackets are replaced by curly brackets, and each subexpression is prefixed with a
property name and a colon:
e.g.
• xx
var p = { x:2.3, y:-1.2 }; // An object with 2 properties
var q = {}; // An empty object with no properties
q.x = 2.3; q.y = -1.2; // Now q has the same properties as p
A function definition expression can also include a name for the function.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
29
MHSSP
Expressions in Javascript
• Property Express Expressions:
• A property access expression evaluates to the value of an object
property or an array element.
o expression . identifier
o expression [ expression ]
• The expression specifies the object, and the identifier specifies the name of
the desired property.
• The second style of property access follows the first expression (the object or
array) with another expression in square brackets.
• This second expression specifies the name of the desired property of the index of
the desired array element.
• In method invocations, the object or array that is the subject of the property
access becomes the value of the this parameter while the body of the function
is being executed.
• This enables an object-oriented programming paradigm in which functions
(known by their OO name, “methods”) operate on the object of which they
are part.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
36
MHSSP
Control Statements in Javascript
• If statement:
• The if statement is the fundamental control statement that
allows JavaScript to make decisions, or, more precisely, to
execute statements conditionally.
Or similarly:
if (expression)
statement1
else
Statement2
• E.g.
if (n == 1)
console.log("You have 1 new message.");
else
console.log("You have " + n + " new messages.");
• But what about when you need to execute one of many pieces of code? One
way to do this is with an else if statement.
• This is not the best solution, however, when all of the branches depend on the
value of the same expression.
while (expression)
statement
• To execute a while statement, the interpreter first evaluates expression. If the
value of the expression is false, then the interpreter skips over the statement
that serves as the loop body and moves on to the next statement in the
program.
• If, on the other hand, the expression is true, the interpreter executes the
statement and repeats, jumping back to the top of the loop and evaluating
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
expression again. MHSSP
46
Loop Statements in Javascript
• do while Loop:
• The do/while loop is like a while loop, except that the loop
expression is tested at the bottom of the loop rather than at
the top. This means that the body of the loop is always executed at least once.
The syntax is:
do
statement
while (expression);
• There are a couple of syntactic differences between the do/while loop and the
ordinary while loop.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
47
MHSSP
Loop Statements in Javascript
• do while Loop:
• First, the do loop requires both the do keyword (to mark the
beginning of the loop) and the while keyword (to mark the end
and introduce the loop condition).
• Also, the do loop must always be terminated with a semicolon. The while loop
doesn’t need a semicolon if the loop body is enclosed in curly braces.
• Most loops have a counter variable of some kind. This variable is initialized
before the loop starts and is tested before each iteration of the loop. Finally,
the counter variable is incremented or otherwise updated at the end of the
loop body, just before the variable is tested again.
• In this kind of loop, the initialization, the test, and the update are the three
crucial manipulations of Prepared
a loop variable.
By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
49
MHSSP
Loop Statements in Javascript
• for Loop:
• The for statement encodes each of these three manipulations
as an expression and makes those expressions an explicit
part of the loop syntax:
• It is easy to use a regular for loop to iterate through the elements of an array:
• The for/in loop makes it easy to do the same for the properties of an object:
• If it evaluates to null or undefined, the interpreter skips the loop and moves
on to the next statement.
break;
break label_name;
• When break is used with a label, it jumps to the end of, or terminates,
the enclosing statement that has the specified label.
• The new operator creates and initializes a new object. The new
keyword must be followed by a function invocation.
• All objects created by object literals have the same prototype object, and we
can refer to this prototype object in JavaScript code as Object.prototype.
• Objects created using the new keyword and a constructor invocation use the
value of the prototype property of the constructor function as their prototype.
• So the object created by new Object() inherits from Object.prototype just as
the object created by {} does.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
63
MHSSP
Creating Objects in JavaScript
• Object.create() :
• It creates a new object, using its first argument as the prototype
of that object.
• It also takes an optional second argument that describes the properties of the
new object.
E.g. var o1 = Object.create({x:1, y:2}); // o1 inherits properties x and y.
• If you want to create an ordinary empty object (like the object returned by {}
or new Object()), pass Object.prototype:
var title = book["main title"] // Get the "main title" property of the book.
Book.author=company.employee;
• Syntax:
get.identifier()
• Syntax:
set.identifier(argument)