Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger


Sunday 14 September 2008

Javascript Interview Questions and Answers 13

How to associate functions with objects using JavaScript?
Let's now create a custom "toString()" method for our movie object. We can embed the function directly in the object like this.


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.


This produces
title: Aliens director: Cameron

eval()?
The eval() method is incredibly powerful allowing you to execute snippets of code during execution.

This produces
Population is 521,289

What does break and continue statements do?
Continue statement continues the current loop (if label not specified) in a new iteration whereas break statement exits the current loop.

How to create a function using function constructor?
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");

What's Prototypes for JavaScript?
Objects have "prototypes" from which they may inherit fields and functions.

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



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

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

decodeURI(), encodeURI()
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.

No comments:

Post a Comment