Set - 7

Question 1 :

What's the Date object using JavaScript? 

Answer :

Time inside a date object is stored as milliseconds since Jan 1, 1970.

new Date(06,01,02) // produces "Fri Feb 02 1906 00:00:00 GMT-0600 (Central Standard Time)" 
new Date(06,01,02).toLocaleString() // produces "Friday, February 02, 1906 00:00:00" 
new Date(06,01,02) - new Date(06,01,01) // produces "86400000"


Question 2 :

What does the delete operator do?

Answer :

The delete operator is used to delete all the variables and objects used in the program ,but it does not delete variables declared with var keyword.


Question 3 :

How to create Arrays using JavaScript ?

Answer :

<script type="text/javascript">
var days = new Array();
days[0] = "Sunday"
days[1] = "Monday"
days[2] = "Tuesday"
days[3] = "Wednesday"
days[4] = "Thursday"
days[5] = "Friday"
days[6] = "Saturday"
document.write("first day is "+days[0])
</script>

This produces
first day is Sunday
A more compact way of creating an array is the literal notation: 

<script type="text/javascript">
var days = ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"];
document.write("first day is "+days[0]) 
</script>

This produces
first day is Sunday


Question 4 :

How to delete an entry using JavaScript? 

Answer :

The "delete" operator removes an array element, but oddly does not change the size of the array.

<script type="text/javascript">
var days = ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"];
document.write("Number of days:"+days.length); delete days[4];
document.write("<br />Number of days:"+days.length);
</script>

This produces
Number of days:7
Number of days:7


Question 5 :

How to use strings as array indexes using JavaScript?

Answer :

Javascript does not have a true hashtable object, but through its wierdness, you can use the array as a hashtable.

<script type="text/javascript">
var days = ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"];
for(var i=0; i < days.length; i++) {
	days[days[i]] = days[i];
}
document.write("days[\"Monday\"]:"+days["Monday"]);
</script>

This produces
days["Monday"]:Monday


Question 6 :

How to use "join()" to create a string from an array using JavaScript?

Answer :

"join" concatenates the array elements with a specified seperator between them.

Explanation :

<script type="text/javascript">
	var days = ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"];
	document.write("days:"+days.join(","));
</script>

This produces
days:Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday


Question 7 :

How to make a array as a stack using JavaScript?

Answer :

The pop() and push() functions turn a harmless array into a stack

<script type="text/javascript">
	var numbers = ["one", "two", "three", "four"];
	numbers.push("five");
	numbers.push("six");
	document.write(numbers.pop());
	document.write(numbers.pop());
	document.write(numbers.pop());
</script>

This produces
sixfivefour


Question 8 :

How to shift and unshift using JavaScript?

Answer :

<script type="text/javascript">
	var numbers = ["one", "two", "three", "four"];
	numbers.unshift("zero");
	document.write(" "+numbers.shift());
	document.write(" "+numbers.shift());
	document.write(" "+numbers.shift());
</script>

This produces
zero one two
shift, unshift, push, and pop may be used on the same array. Queues are easily implemented using combinations.


Question 9 :

How to create an object using JavaScript?

Answer :

Objects can be created in many ways. One way is to create the object and add the fields directly.

<script type="text/javascript">
	var myMovie = new Object(); 
	myMovie.title = "Aliens";
	myMovie.director = "James Cameron";
	document.write("movie: title is \""+myMovie.title+"\"");
</script>

This produces
movie: title is "Aliens"
To create an object you write a method with the name of your object and invoke the method with "new".

<script type="text/javascript">
function movie(title, director) {
	this.title = title;
	this.director = director;
}
var aliens = new movie("Aliens","Cameron");
document.write("aliens:"+aliens.toString());
</script>

This produces
aliens:[object Object]

You can also use an abbreviated format for creating fields using a ":" to separate the name of the field from its value. This is equivalent to the above code using "this.".

<script type="text/javascript">
function movie(title, director) {
	title : title;
	director : director;
}
var aliens = new movie("Aliens","Cameron");
document.write("aliens:"+aliens.toString());
</script>

This produces
aliens:[object Object] 


Question 10 :

How to associate functions with objects using JavaScript?

Answer :

Let's now create a custom "toString()" method for our movie object. We can embed the function directly in the object like this.

<script type="text/javascript">
function movie(title, director) {
	this.title = title;
	this.director = director;
	this.toString = function movieToString() {
		return("title: "+this.title+" director: "+this.director);
	}
}
var narnia = new movie("Narni","Andrew Adamson");
document.write(narnia.toString());
</script>

This produces
title: Narni director: Andrew Adamson

Or we can use a previously defined function and assign it to a variable. Note that the name of the function is not followed by parenthesis, otherwise it would just execute the function and stuff the returned value into the variable.

<script type="text/javascript">
function movieToString() {
	return("title: "+this.title+" director: "+this.director);
}
function movie(title, director) {
	this.title = title;
	this.director = director;
	this.toString = movieToString; //assign function to this method pointer
}
var aliens = new movie("Aliens","Cameron");
document.write(aliens.toString());
</script>

This produces
title: Aliens director: Cameron


Question 11 :

eval()?

Answer :

The eval() method is incredibly powerful allowing you to execute snippets of code during execution.
<script type="text/javascript">
var USA_Texas_Austin = "521,289";
document.write("Population is "+eval("USA_"+"Texas_"+"Austin"));
</script>
This produces
Population is 521,289


Question 12 :

What does break and continue statements do?

Answer :

Continue statement continues the current loop (if label not specified) in a new iteration whereas break statement exits the current loop.


Question 13 :

How to create a function using function constructor? 

Answer :

The following example illustrates this
It creates a function called square with argument x and returns x multiplied by itself.
var square = new Function ("x","return x*x");


Question 14 :

What's Prototypes for JavaScript?

Answer :

Objects have "prototypes" from which they may inherit fields and functions.

<script type="text/javascript">
function movieToString() {
return("title: "+this.title+" director: "+this.director);
}
function movie(title, director) {
this.title = title;
this.director = director || "unknown"; //if null assign to "unknown"
this.toString = movieToString; //assign function to this method pointer
}
movie.prototype.isComedy = false; //add a field to the movie's prototype
var officeSpace = new movie("OfficeSpace");
var narnia = new movie("Narni","Andrew Adamson");
document.write(narnia.toString());
document.write("
Narnia a comedy? "+narnia.isComedy);
officeSpace.isComedy = true; //override the default just for this object
document.write("
Office Space a comedy? "+officeSpace.isComedy);
</script>


Question 15 :

unescape(), escape()

Answer :

These are similar to the decodeURI() and encodeURI(), but escape() is used for only portions of a URI.

<script type="text/javascript">
var myvalue = "Sir Walter Scott";
document.write("Original myvalue: "+myvalue);
document.write("<br />escaped: "+escape(myvalue));
document.write("<br />uri part: \"&author="+escape(myvalue)+"\"");
</script>

If you use escape() for the whole URI... well bad things happen.

<script type="text/javascript">
var uri = "http://www.google.com/search?q=sonofusion Taleyarkhan"
document.write("Original uri: "+uri);
document.write("
escaped: "+escape(uri));
</script>

 


Question 16 :

decodeURI(), encodeURI()

Answer :

Many characters cannot be sent in a URL, but must be converted to their hex encoding. These functions are used to convert an entire URI (a superset of URL) to and from a format that can be sent via a URI.

<script type="text/javascript">
	var uri = "http://www.google.com/search?q=sonofusion Taleyarkhan"
	document.write("Original uri: "+uri);
	document.write("<br />encoded: "+encodeURI(uri));
</script>