CSS Chapter 1
CSS Chapter 1
JavaScript
Programmin
Unit - I
g
•Applications of Programming
Languages :
oThey typically run inside a parent
program like scripts
oMore compatible while integrating
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
MHSSP
4
Client Side Vs Server Side
Scripting
•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 )
•(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.
•On the other hand, variables declared within a function are defined
only within the body of the function. They are local variables and have
local scope.
• 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
o expression . identifier
expression [ expression ]
o
• 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
expression again. Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
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.
• 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
•for Loop: Javascript
• The for statement encodes each of these three manipulations
as an expression and makes those expressions an explicit
part of the loop syntax:
• The for/in loop makes it easy to do the same for the properties of an
object:
for(var p in o) // AssignPrepared
property names
By: Khan Mohammed of
Zaid, Lecturer,
MHSSP
o to
Comp. variable p
Engg.,
52
Loop Statements in
•for in Loop: Javascript
• To execute a for/in statement, the JavaScript interpreter first
evaluates the object expression.
• 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:
•If using the dot operator, the right-hand must be a simple identifier
that names the property.
var title = book["main title"] // Get the "main title" property of the book.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
66
MHSSP
Querying & Setting Properties in
JavaScript
•To create or set a property, use a dot or square brackets as
you would to query the property, but put them on the
left-hand side of an assignment expression:
Book.author=company.employee;
•It is an accessor property, that acts as a function but look like regular
properties to an external code.
• Syntax:
get.identifier()
• Syntax:
set.identifier(argument)