Tuesday, August 10, 2010

Javascript Interview Questions - Part5

What is the difference between the three following scenarios:

{code type = "javascript"}
var myFunction = function()
{
var person;
person;
window.person;
}

{/code}
The first is local variable, the last two are global.

How do you create an object in JavaScript?
var person = new Object; or var person = {};

How to you assign a value to an object?
person["name"] = "dave"; or person.name = "dave";

What is the difference between object and constructor in JavaScript?
Constructor works like a class, and before using, we need to create an instance. Object can be accessed directly: no instance creation.

How do you create a local and global method in a constructor?

{code type = "javascript"}
var myClass = function()
{
var PrivatMethod = function()
{
}

this.PublicMethod = function(){

}

}

{/code}

Have you heard of Douglas Crockford?
Yes he is the "inventor" of JSON.

How do you inherit from one object to another?
var Object1 = {someProp : "some value", someOtherProperty : "some other Value"};var Object2 = {someProp2 : "some value", someOtherProperty2 : "some other Value"};Object1.prototype = new Object2;//now we can alertalert(Object1.someProp2);

What debugging tools do you use?
Firebug in Firefox (console.log(); method) and Visual Studio in IE ('debug' command inserted in javascript file)

How do you delete the last element of an array in JavaScript?
array_object_variable.pop();
The pop() method in JavaScript will delete the last element of the array. Whereas the push() method will add a new element at the start of the array.

How do you get the random number between 0 and 50?
my_js_random_number = Math.floor(Math.random() * 51);
In the similar manner you can generate random number from 0 to N, you need to pass N+1 instead of 51 in this function.

How do you redirect a page using JavaScript?
To redirect to you new page or URL in JavaScript, we can use the location object.
location = ‘newpage.html’

How do you refresh the current page using JavaScript?
To refresh the current page using a javascript function, we can use the below method:
location.reload();

This entry was originally published at Experts Support Blog

No comments:

Post a Comment