Set - 7

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