Friday, 23 October 2015

Object Oriented JavaScript interview questions and answers for experienced

Object Oriented JavaScript interview questions and answers for experienced


Question: Is JavaScript case sensitive?
Yes, JavaScript is a case sensitive..


Question:What are different Data-Types of JavaScript?
Following are different data-type in JavaScript.
  1. String
  2. Number
  3. Boolean
  4. Function
  5. Object
  6. Null
  7. Undefined



Question: What is an Object?
The object is a collection of properties & each property associated with the name-value pairs.
The object can contain any data types (numbers, string, arrays, object etc.).


Question: What are different two ways of creating an object?
Object Literals: This is the most common way to create the object with object literal.
For Example:
var emptyObj= {};

Object Constructor: It is way to create object using object constructor and the constructor is used to initialize new object.
For Example:
Var obj = new Object();


Question: What is scope variable in JavaScript?
The scope is set of objects which can be variables and function.
"Scope variable" can be have global scope variable and local scope variable.



Question: Give an example creating Global variable
Global variable: A variable which can be variable from any where of the page.
Following are different two ways.
First Way Declare the JavaScript variable at the top of JavaScript code and out of function & objects.
var globalVariable1 ='This is global variable 1'

Second WayDeclare a varaible without "var" in Function.
function testfunction(){
    globalVariable2 ='This is global variable 2'
}



Question: Give an example creating Global variable
Local variable: A variable which can be local and can't access globally.
When we declare a local varriable, Its local and can't access globally. It must create using "var" keyword.
function testfunction1(){
    var localVariable ='This is local variable '
}


Question: What is public, private and static variables in JavaScript?
Public Varaible: A variable which associate to object and is publicily available with object.
For Example:
function funcName1 (name) {
 this.publicVar='1'; 
}

Private Variable: A variable which associate to object and is limited available.
For Example:
function funcName2 (name) {
 var privateVar='1'; 
}

Static variable: A static member is shared by all instances of the class as well as the class itself and only stored in one place.
For Example:
function funcName3 (name) {
 
}
// Static property
funcName3.name = "Web Technology Experts Notes";



Question: How to achieve inheritance in JavaScript
"Pseudo classical inheritance" and "Prototype inheritance"


Question: What is closure in JavaScript?
When we create the JavaScript function within another function and the inner function freely access all the variable of outer function.


Question: What is prototype in JavaScript?
All the JavaScript objects has an object and its property called prototype & it is used to add and the custom functions and property. See Following example in which we create a property and function.
var empInstance = new employee();
empInstance.deportment = "Information Technology";
empInstance.listemployee = function(){

}



3 comments :

પ્રદિપ આસોદરીયા વાડલા (ગીર) said...

Helpful Article Thanks.

Suresh Kumar said...

Question: Give an example creating Global variable
Local variable: A variable which can be local and can't access globally.
When we declare a local varriable, Its local and can't access globally. It must create using "var" keyword.


Question should be "Give an example creating Local variable"

Typo error

Unknown said...

A globally-scoped variable
var a = 1;

// global scope
function one() {
alert(a); // alerts '1'
}
Local scope
var a = 1;

function two(a) {
alert(a); // alerts the given argument, not the global value of '1'
}

// local scope again
function three() {
var a = 3;
alert(a); // alerts '3'
}
Intermediate: No such thing as block scope in JavaScript (ES5; ES6 introduces let)

a.

var a = 1;

function four() {
if (true) {
var a = 4;
}

alert(a); // alerts '4', not the global value of '1'
}
b.
var a = 1;

function one() {
if (true) {
let a = 4;
}

alert(a); // alerts '1' because the 'let' keyword uses block scoping
}
Intermediate: Object properties
var a = 1;

function five() {
this.a = 5;
}

alert(new five().a); // alerts '5'
Advanced: Closure
var a = 1;

var six = (function() {
var a = 6;

return function() {
// JavaScript "closure" means I have access to 'a' in here,
// because it is defined in the function in which I was defined.
alert(a); // alerts '6'
};
})();
Advanced: Prototype-based scope resolution
var a = 1;

function seven() {
this.a = 7;
}

// [object].prototype.property loses to
// [object].property in the lookup chain. For example...

// Won't get reached, because 'a' is set in the constructor above.
seven.prototype.a = -1;

// Will get reached, even though 'b' is NOT set in the constructor.
seven.prototype.b = 8;

alert(new seven().a); // alerts '7'
alert(new seven().b); // alerts '8'
Global+Local: An extra complex Case

var x = 5;

(function () {
console.log(x);
var x = 10;
console.log(x);
})();
This will print out undefined and 10 rather than 5 and 10 since JavaScript always moves variable declarations (not initializations) to the top of the scope, making the code equivalent to:
var x = 5;

(function () {
var x;
console.log(x);
x = 10;
console.log(x);
})();
Catch clause-scoped variable

var e = 5;
console.log(e);
try {
throw 6;
} catch (e) {
console.log(e);
}
console.log(e);
This will print out 5, 6, 5. Inside the catch clause e shadows global and local variables. But this special scope is only for the caught variable. If you write var f; inside the catch clause, then it's exactly the same as if you had defined it before or after the try-catch block

Post a Comment