x = 1;
var a = 5;
var b = 10;
var c = function(a, b, c) {
document.write(x); //i
document.write(a); //ii
var f = function(a, b, c) {
b = a;
document.write(b); //iii
b = c;
var x = 5;
}
f(a,b,c);
document.write(b); //iv
var x = 10;
}
c(8,9,10);
document.write(b);
document.write(x);
Answers:
undefined //i x
8 //ii a
8 //ii b
9 //iv b
Global Scope: In Javascript, there is one global scope. Variable defined in that scope can be accesed anywhere in file.
Local Scope: Function define its own scope called local scope. The variable defined in that scope are only accessible to that scope.
// Scope A
function XFunc () {
// Scope B
function YFunc () {
// Scope C
};
};
(a) Do statements in Scope A have access to variables defined in Scope B and C?
(b) Do statements in Scope B have access to variables defined in Scope A?
(c) Do statements in Scope B have access to variables defined in Scope C?
(d) Do statements in Scope C have access to variables defined in Scope A?
(e) Do statements in Scope C have access to variables defined in Scope B?
Answer:
(a). false
(b). true
(c). false
(d). true
(e). true
var x = 9;
function myFunction() {
return x * x;
}
document.write(myFunction());
x = 5;
document.write(myFunction());
Answers:
81
25
var foo = 1;
function bar() {
if (!foo) {
var foo = 10;
}
alert(foo);
}
bar();
Answer:
10var add = (function () {
var counter = 0;
return function () {
return counter += 1;
}
})();
Answer:
var count = {
counter: 0,
add: function (input) {
if (input) {
this.counter += input;
}
else {
this.counter++;
}
return this.counter;
},
reset: function () {
this.counter = 0;
},
};
Answer:
In the cntext of javascript closure, free variables are those variables that a child scope can use because those variable were declared in the enclosing or parent scope.Answer:
var add = (function(){
let counter = 0;
return function(input){
if(input){
counter = counter+input;
}
else{
counter = counter+1;
}
return counter;
}
})();
var make_adder = function (inc) {
return function () {
return add(inc);
};
};
Answer:
Using Module pattern we can remove all the names from global namespace.
Private Field: name
Private Field: age
Private Field: salary
Public Method: setAge(newAge)
Public Method: setSalary(newSalary)
Public Method: setName(newName)
Private Method: getAge( )
Private Method: getSalary( )
Private Method: getName( )
Public Method: increaseSalary(percentage) // uses private getSalary( )
Public Method: incrementAge( ) // uses private getAge( )
Answer:
var Employee = (function () {
var name, age, salary;
var getAge = function () {
return age;
};
var getSalary = function () {
return salary;
};
var getAgePubFunc = function () {
return getAge();
};
var getSalaryPubFunc = function(){
return getSalary();
};
var setAge = function (newAge) {
age = newAge;
};
var setSalary = function(newSalary){
salary = newSalary;
};
var setName = function(newName){
name = newName;
};
var increaseSalary = function(percentage){
var sal = getSalary();
setSalary(sal + (sal*percentage)*0.01);
};
var incrementAge = function(){
var newAge = getAge() + 1;
setAge(newAge);
};
return {
setAge: setAge,
setName:setName,
setSalary:setSalary,
increaseSalary:increaseSalary,
incrementAge: incrementAge
};
})();
Answer:
var Employee = (function () {
var name, age, salary;
var getAge = function () {
return age;
};
var getSalary = function () {
return salary;
};
return {
setAge: function(newAge){
age = newAge;
},
setName:function(newName){
name = newName;
},
setSalary: function(newSalary) {
salary = newSalary;
},
increaseSalary: function(percentage){
var sal = getSalary();
setSalary(sal + (sal*percentage)*0.01);
},
incrementAge: function(){
var newAge = getAge() + 1;
setAge(newAge);
}
};
})();
Answer:
var Employee = (function () {
var name, age, salary;
let getAge = function () {
return age;
};
let getSalary = function () {
return salary;
};
let employeeObj = {};
employeeObj.setAge = function(newAge) {
age = newAge;
};
employeeObj.setName = function(newName) {
name = newName;
};
employeeObj.setSalary = function(newSalary) {
salary = newSalary;
};
employeeObj.increaseSalary= function(percentage){
var sal = getSalary();
setSalary(sal + (sal*percentage)*0.01);
};
employeeObj.incrementAge = function(){
var newAge = getAge() + 1;
setAge(newAge);
};
};
return employeeObj;
})();
Answer:
Employee.address = "";
Employee.setAddress = function(newAddress) {
this.setAddress = newAddress;
};
Employee.getAddress = function() {
return this.address;
};
const promise = new Promise((resolve, reject) => {
reject(“Hattori”);
});
promise.then(val => alert(“Success: “ + val))
.catch(e => alert(“Error: “ + e));
Answers:
Error: Hattori
const promise = new Promise((resolve, reject) => {
resolve(“Hattori”);
setTimeout(()=> reject(“Yoshi”), 500);
});
promise.then(val => alert(“Success: “ + val))
.catch(e => alert(“Error: “ + e));
Answer:
Succes: Hattori
function job(state) {
return new Promise(function(resolve, reject) {
if (state) {
resolve('success');
} else {
reject('error');
}
});
}
let promise = job(true);
promise.then(function(data) {
console.log(data);
return job(false);})
.catch(function(error) {
console.log(error);
return 'Error caught';
});
Answer:
sucess
error
Error caught