13

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

6/23/2014

Java Script Interview Questions And Answers

Home Interview Questions Certifications Aptitude Questions Tutorials Placement Papers Search
Resume Soft Skills Video Forum Blog

Google Search

Technical Interview Questions

Oracle Interview Questions


J2EE Interview Questions
C++ Interview Questions
XML Interview Questions
EJB Interview Questions
JSP Interview Questions
.........More

Javascript Interview Questions and Answers

Programming Source Codes

Java Source Codes


Html Source Codes
CSS Source Codes
C Source Codes
.........More

Soft Skills

Communication Skills
Leadership Skills
.........More

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

http://techpreparation.com/computer-interview-questions/javascript-interview-questions-answers13.htm#.U6gvy_mSwiQ

1/6

6/23/2014

Java Script Interview Questions And Answers

Calling Web
Developers
modern.ie

Visit Modern.IE, View and Test Your Site


Across Browsers & OS.

1 Yr Online MBA
Rs.7500/Naukri.com Register Now
Jobs in TCS
Are You a Fresher?
Part Time Mba
Program
Speak English
Fluently
Upload Your CV For
Jobs

http://techpreparation.com/computer-interview-questions/javascript-interview-questions-answers13.htm#.U6gvy_mSwiQ

2/6

6/23/2014

Java Script Interview Questions And Answers

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) {
http://techpreparation.com/computer-interview-questions/javascript-interview-questions-answers13.htm#.U6gvy_mSwiQ

3/6

6/23/2014

Java Script Interview Questions And Answers

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
eval()?

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
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.
<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
http://techpreparation.com/computer-interview-questions/javascript-interview-questions-answers13.htm#.U6gvy_mSwiQ

4/6

6/23/2014

Java Script Interview Questions And Answers

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>
unescape(), escape()

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));
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.
<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>
Page Numbers :

http://techpreparation.com/computer-interview-questions/javascript-interview-questions-answers13.htm#.U6gvy_mSwiQ

8
5/6

6/23/2014

Java Script Interview Questions And Answers

10

11

12

13

Have a Question ? post your questions here. It will be answered


as soon as possible.
Check Java Interview Questions for more Java Interview
Questions with answers
Check Structs Interview Questions for more Structs Interview
Questions with answers
Check Servlet Interview Questions for more Servlet Interview
Questions with answers

35

http://techpreparation.com/computer-interview-questions/javascript-interview-questions-answers13.htm#.U6gvy_mSwiQ

6/6

You might also like