Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger


Sunday 14 September 2008

Javascript Interview Questions and Answers 12

How to use strings as array indexes using JavaScript?
Javascript does not have a true hashtable object, but through its wierdness, you can use the array as a hashtable.


This produces
days["Monday"]:Monday

How to use "join()" to create a string from an array using JavaScript?
"join" concatenates the array elements with a specified seperator between them.


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

How to make a array as a stack using JavaScript?
The pop() and push() functions turn a harmless array into a stack


This produces
sixfivefour

How to shift and unshift using JavaScript?

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

How to create an object using JavaScript?
Objects can be created in many ways. One way is to create the object and add the fields directly.


var myMovie = new Object();
myMovie.title = "Aliens";
myMovie.director = "James Cameron";
document.write("movie: title is \""+myMovie.title+"\"");
<
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".

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.".

This produces
aliens:[object Object]

No comments:

Post a Comment