|
ECMAScript/JavaScript
An Introduction for Programmers |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
// and the end of the line
is a comment/* and a */
is a commenttrue or false
Infinity, NaN,
Number.MAX_VALUE, Number.MIN_VALUE,
Number.NaN, Number.POSITIVE_INFINITY,
Number.NEGATIVE_INFINITY
\ is the escape character and can be used to
represent special characters (e.g., \n represents
the newline character) and Unicode characters
(e.g., \u00e9 is é)
var cost; var cost=5; var cost=5, price=6.20;
undefined
+)-)*)/)/)%)++) and Decrement (--)-)>
<
>=
<=
==
===
!=
!==
NaN (i.e., NaN === NaN
always evaluates to false)null == undefined evaluates to true
true becomes 1 and
false becomes 0)
&&
||
!
if Statementwhile Loopsdo-while Loopsfor Loops
var empty = {};
var origin = {x:0, y:0};
var course = {dept: "CS", "number": 139+20};
new Operator:
var today = new Date();
.
horizontalPosition = origin.x;
var position = {x:0, y:0};
position.x = 10;
var position = {};
position.x = 10;
in Operator:
delete Operator:
var empty = []; var grades = [90, 85, 73, 88]; var bad_idea = ["CS", 139, 4, "B-"];
[] Operator:
var a = [2, 4, 6]; a[9] = 20;
delete Operator:
undefined
length Property:
for of Loops[] operator more than oncetoString() method) and then test| Type | Passed By | Compared By |
| boolean | Value | Value |
| number | Value | Value |
| Object | Reference | Reference |
| string | Immutable | Value |
function area(width, height)
{
var result;
result = 0;
if ((width > 0) && (height > 0)) result = width * height;
return result;
}
f):
var f = function(width, height)
{
var result;
result = 0;
if ((width > 0) && (height > 0)) result = width * height;
return result;
};
var size = area(10.2, 20.9);
undefined
function area(width, height)
{
return width*height;
}
var f = area;
Note: This is very different from, for example, var size =
area(3,5); which executes area.
f is the same function as area and can be
used the same way.
f = function(width, height){return width * height;};
a = f(4,5);
// Assign an object literal to a variable
var r = {
width: 10;
height: 5;
area: function()
{
return this.width * this.height;
}
}
// Use the object
var a = r.area();
this?this
refers to the global objectthis
refers to the "owning" objectnew Operator:
this to
refer to the object, and then invokes a functionRectangle Pseudoclass
function Rectangle(width, height)
{
this.width = width;
this.height = height;
this.area = function()
{
return this.width * this.height;
}
}
width and height:
width
and height
area:
area
functionRectangle Pseudoclass
// The Constructor
function Rectangle(width, height)
{
this.width = width;
this.height = height;
}
// The area() Method
Rectangle.prototype.area = function()
{
return this.width * this.height;
}
hasOwnProperty() method can be
used to determine if the property is owned
by the object or its prototypein operator can be used to determine
if an object has or appears to have a property
var r = new Rectangle(10, 5);
r.hasOwnProperty("width"); // Evaluates to true
r.hasOwnProperty("area"); // Evaluates to false
"area" in r; // Evaluates to true
Boolean, Number and
String).
s containing a string,
you can use methods like s.charAt(index)
n containing a number,
you can use methods like n.toString(base)