Js 1
Js 1
Java uses the concept of classes and there is no such thing in JavaScript.
objects that makes reuse of the code
easier
Similarity between java & JavaScript
• Syntax of their expressions , assignment
statements & control statements
Uses of JavaScript
• Provide programming capability at both server & the
client ends of a web connection
• Provide alternative to server-side programming
– Servers are often overloaded
– Client processing has quicker reaction time
• JavaScript can work with forms
• JavaScript can interact with the internal model of the
web page (Document Object Model)
• JavaScript is used to provide more complex
user interface than plain forms with HTML/CSS can
provide
Event-Driven Computation
• Users actions, such as mouse clicks and
key presses, are referred to as events
• The main task of most JavaScript programs is
to respond to events
• For example, a JavaScript program could
validate data in a form before it is submitted
to a server
XHTML/JavaScript Documents
• When JavaScript is embedded in an XHTML document, the
browser must interpret it
• Two locations for JavaScript serve different purposes
– JavaScript in the head element will react to user input and
be called from other locations
– JavaScript in the body element will be executed once as
the page is loaded
Object Orientation and JavaScript
• Not an object-oriented programming language
• Object-based language
• Doest not have classes
• Cannot have class based inheritance
• Prototype – based inheritance
• Cannot supports polymorphism
JavaScript Objects (1)
• Objects are collections of properties
• Properties are either data properties or method properties
• Data properties are either primitive values or references to
other objects.
• primitive value is a value that has no properties or
methods.
• Primitive values are often implemented directly in hardware
resulting in faster operations on their values
• Root object in JavaScript is Object
• The Object object is the ancestor of
all objects in a JavaScript program
– Object has no data properties, but several method
properties
JavaScript in XHTML
• Directly embedded
<script type=“text/javascript”>
<!--
…Javascript here…
-->
</script>
• Indirect reference
<script type=“text/javascript”
src=“hello.js”/>
– This is the preferred approach
JavaScript identifiers
• Identifiers or names are similar to those of
other common programming languages
• Must begin with a letter, an underscore or a
dollar sign
• No length limitations for identifiers
• Case sensitive
JavaScript reserved words
• 25 reserved words
• Break,case,catch,continue,delete,do,return,sw
itch, for, new, while etc
• Another collection of words is reserved for
future use in Javascript
• Has a large collection of predefined words,
including alert,open ,java & self
JavaScript comments
1. two adjacent slashes(//) appear on a line
,the rest of the line is considered as comment
2. /*…………………*/ (single & multiple line
comments)
Issues in embedding JavaScript in
XHTML document
• There are some browsers still in use
recognize the <script> tag but do not have
JavaScript interpreters
– Simply ignore the contents of the script element &
cause no problems.
• Old browsers does not recognize the script
tag, simply read as text
• So enclose the contents of all script elements
in XHTML comments to avoid this problem
JavaScript in XHTML: CDATA (1)
• The <![CDATA[ … ]]> block is intended to hold data
that should not be interpreted as XHTML
• Using this should allow any data (including special symbols
and --) to be included in the script
• This, however does not work, at least in Firefox:
<script type=“text/javascript”>
<![CDATA[
…JavaScript here…
]]>
</script>
• The problem seems to be that the CDATA tag causes an
internal JavaScript error
JavaScript in XHTML: CDATA (2)
• This does work in Firefox
<script type=“text/javascript”>
/*<![CDATA[ */
…JavaScript here…
/*]]> */
</script>
• Property value
firstName John
lastName Doe
fullName functi
on()
{retur
n
• JavaScript objects are containers for named
this.fir
values, calledstNam
properties and methods.
e
Wrapper Objects (1)
• JavaScript includes predefined objects
• that are closely related to the Number,
String, and Boolean types, named Number,
String, and Boolean,
• These objects are called wrapper objects.
Wrapper Objects (2)
• Each contains a property that stores a value of the
corresponding primitive type.
• purpose of the wrapper objects is to provide
properties and methods that are convenient for use with values of
the primitive types.
In the case of Number, the properties are more useful; in the case of String,
methods are more useful
• Because JavaScript coerces values between the Number
type primitive values and Number objects and between the
String type primitive values and String objects
• methods of Number and String can be used on variables
of the corresponding primitive types.
The difference between primitives and objects is
shown in the following example.
• prim is a primitive variable
with the value 17
• obj is a Number object
whose property value is 17.
• Figure shows how prim and
obj are stored.
Numeric and String Literals
• Number values are represented internally as
double-precision floating-point values
– Number literals can be either integer or float
– Float values may have a decimal and/or and exponent
• Undefined
– A single value, undefined
– However, undefined is not, itself, a reserved word
– The value of a variable that is declared but not assigned a value
• Boolean
– Two values: true and false
Declaring Variables
• JavaScript is dynamically typed, that is,
variables do not have declared types
– A variable can hold different types of values at different times during program
execution
Operators Associativity
++, --, unary - Right
*, /, % Left
+, - Left
>, <, >= ,<= Left
==, != Left
===,!== Left
&& Left
|| Left
=, +=, -=, *=, /=, &&=, ||=, %= Right
The Math Object
• Character positions in
strings begin at index
0
String Methods
• Comparison operators
== != < <= > >=
=== compares identity of values or objects
3 == ‘3’ is true due to automatic conversion
3 === ‘3’ is false (checking the data type also)
• Boolean operators
&& || !
switch (expression)
{
case value_1:
// statement(s)
case value_2:
// statement(s)
...
[default:
// statement(s)]
}
switch Statement Semantics
• For
for (initial expression; control expression; increment
expression) statement or compound statement
• do/while
do statement or compound statement
while (control expression)
4.7 Object Creation and
Modification
• The new expression is used to create an object
– This includes a call to a constructor
– The new operator creates a blank object, the constructor creates and
initializes all properties of the object
• Syntax
for (identifier in object)
statement or compound statement
• Result:
Name: make; Value: Ford
Name: model; Value: Contour SVT
4.8 Arrays
• push
– Add to the end
• pop
– Remove from the end
• shift
– Remove from the front
• unshift
– Add to the front
4.8 Dynamic List Operations (2)
• Var list = [“ramu”, “rani”, “raj”];
var deer = list.pop(); // deer is “raj”
list.push(“raj”); // this puts “raj” back to list
• Shift & unshift remove & add an element to
the beginning of an array
var deer = list.shift(); // deer is now “ramu”
list.unshift(“ramu”); // this puts “ramu” back
to list
4.9 Function Fundamentals (1)
• return statements
– A return statement causes a function to cease execution and control to pass to
the caller
– A return statement may include a value which is sent back to the caller
• This value may be used in an expression by the caller
– A return statement without a value implicitly returns undefined
4.9 Function Fundamentals (2)
• Function call syntax
– Function name followed by parentheses and any actual
parameters
– Function call may be used as an expression or part of an
expression
• Functions must defined before use in the page
header
4.9 Functions are Objects