• +91 9971497814
  • info@interviewmaterial.com

Computer Interview Questions Answers

Java Script Interview Questions Answers

Question - 101 : - How to create an object using JavaScript?

Answer - 101 : - 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+"\""); < 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 - 102 : - How to Handle Event Handlers?

Answer - 102 : - You can add an event handler in the HTML definition of the element like this, <script type="text/javascript"><!-- function hitme() { alert("I've been hit!"); } // --> </script> <input type="button" id="hitme" name="hitme" value="hit me" onclick="hitme()" Or, interestingly enough you can just assign the event's name on the object directly with a reference to the method you want to assign. <input type="button" id="hitme2" name="hitme2" value="hit me2"/> <script type="text/javascript"><!-- function hitme2() { alert("I've been hit too!"); } document.getElementById("hitme2").onclick = hitme2; // --> </script> You can also use an anonymous method like this: document.getElementById("hitme3").onclick = function () { alert("howdy!"); } You can also use the W3C addEvventListener() method, but it does not work in IE yet: <input type="button" id="hitme4" name="hitme4" value="hit me4"/> <script type="text/javascript"><!-- function hitme4() { alert("I've been hit four!"); } if(document.getElementById("hitme4").addEventListener) { document.getElementById("hitme4").addEventListener("click", hitme4, false); } // --> </script>

Question - 103 : - What's the Date object using JavaScript?

Answer - 103 : - 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"

Computer Contributors

krishan
Rajeev Katiyar
Yes Baroda

Share your email for latest updates

Name:
Email:

Our partners