CS174
Chris Pollett
Oct. 13, 2010
typeof x //and typeof(x)returns either "boolean", "string", "number" if x is of primitive type, it returns "object" if x is null or an object; and it returns "undefined" if x is not defined.
a++; a+=2; a--; a-=2; a = b +57;
var today = new Date();
name = prompt("What is your name", "John Smith")
var my_object = new Object();
my_object.make= "V6" /* would then give a property make a value. */ //can access as p = my_object["make"] q = my_object.make
my_object.subObject = new Object();
for(var prop in my_object){...}
var myArr = new Array(1, 2, "hello") var myArr = new Array(100); var myArr = [1,2,3]; //to access myArr[0] //to determine length myArr.length
var names = new Array("Mary", "Murray", "Max");
var nstring = names.join(":");
var a = [1, 2, 3]; a.concat(4, 5);
function swap(i, j, a)
{
var tmp=a[i]; /* explicitly defined variables
have scope within the function
if I had declared the variable
implicitly it would have global scope */
a[i] = a[j]; a[j] = tmp;
}
swap(10, 5, b);
var b = swap;
function swap()
{ var i = this.arguments[0], j=this.arguments[1], a=this.arguments[2];
//same code as before
}
function car(new_make, new_model, new_year)
{
this.make = new_make;
this.model = new_model;
this.year = new_year;
}
my_car = new car("Ford", "Contour SVT", "2000");
function display_car()
{
document.write("Make:", this.make, "<br />");
document.write("Model:", this.model, "<br />");
document.write("Year:", this.year, "<br />");
}
function car(/*same as before*/)
{ //same as before
this.display = display_car;}
function car(/*same as before*/)
{ //same as before}
car.prototype.display = display_car;
var str = "Rabbits are furry"; var position = str.search(/bits/); /* returns position of first occurrence */
/yx{5}z/ matches yxxxxxz
/Apple/i would match APPLE, aPple and apple.
var str="Fred, Freddie, Frederica"; str.replace(/Fre/g, "Boyd");notice we use g to replace all occurrences.
var str= "3 and 4"; var matches = str.match(/\d/g); //returns [3, 4]
var str="grapes:apples:oranges"
var fruit = str.split(":"); // [grapes, apples,oranges]