JS As A Programming Language
JS As A Programming Language
as a Programming Language
(Better than C#)
A little history
JavaScript introduced in Netscape
version 2.0B3 December 1995
JScript was included in Internet
Explorer 3.0, August 1996.
ECMA script first edition June 1997
ECMA script third edition December
1999
.NET framework
Release dates of .net framework
1.0 released at 2002/02/13
2.0 released at 2005/11/07
3.0 released at 2007/11/19
JavaScript Present and Future
September 2008 Google released
Chrome browser
http://v8.googlecode.com/svn/data/be
nchmarks/v2/run.html
In 2010 Microsoft will release new
JavaScript implementation based on
DLR
Work on 3.1 and 4.0 editions of the
standards continues
JavaScript is multi-paradigm language
Conversion:
var i=Number(‘1’);
‘1’*1 also works
Strings
Immutable
build from 16 bit characters
It possible to insert character by
Unicode escape codes
“A”==“\u0041”
UCS-2, not quite UTF-16
Array
Can have mix of values of any type
JS arrays are sparse arrays
var arr=[];
arr[2]=1;
arr.length = ?
JS arrays are growing dynamically
Arrays are stacks push(obj,obj,..), pop()
Arrays are queues shift(), unshift(obj,obj,..)
Arrays also objects
arr[‘2’]= ?
Useful build in methods
splice(start,toremove,obj,obj,…)
sort(callback)
slice(start,end)
concat(arr1,arr2,arr3,…)
join(separator)
Demo
Arrays
Function
Have many similarities with objects.
Acting as constructors for objects.
Can be methods of an object
Anonymous function (C# 2.0)
Closures (C# 2.0)
Demo
Functions
JavaScript and OOP
No types == No classes
Type of an object determined by its
behavior (Duck typing) (C# 4.0)
Regular inheritance is not available (You
don’t need it)
Constructors are optional (C# 3.0)
All properties are public (But there is a
trick)
Object
Type of an object is determined by its
value at run time. ( Dynamic Typing )
var obj= {“x”:10,”test”:function(){return
this.x}};
Objects are dictionaries:
obj.x = 10 and obj["x"] = 10 are equivalent
Properties and their values can be added,
changed, or deleted at run-time.
delete obj.x
Constructors and this
this makes sense only when it used
inside member methods of an
object.
Any function can be constructor it
behavior change slightly when it
used as constructor
OOP in JavaScript
Private methods
Privileged methods
Inheritance
new f() produces new object inherited
from f.prototype
Demo
Objects
Inheritance
It’s not obvious that JS has
inheritance
In JS, inheritance is based on
Prototype Chaining and not type
extension
Demo
Inheritance
Small things
a||’default’
a&&a.value
=== , !==
>> << Are slow