What are the different ways in which you can create a javascript object? Choose ALL the right options
Answer
var object = { prop1: "value1", prop2 : "value2"};
var object = new Object();
var object = {};
var object = Object.create({});
Question 2
Question
How to create a constructor function?
Answer
Any function when prefixed with "new" keyword at the time of invocation acts like a constructor function.
Name of the function should be "constructor"
You have to assign a property named "constructor" and assign a function to it
Constructor function cannot be created in javascript
Question 3
Question
How do you demonstrate inheritance in Javascript? Consider "BaseClass" is the name of the parent class and "SubClass" is the name of the sub-class.
Answer
class SubClass extends BaseClass { }
function SubClass() {
BaseClass.apply(this, Array.prototype.slice.call(arguments, 0);
}
SubClass.prototype = Object.create(BaseClass.prototype);
function SubClass() {
BaseClass.apply(this, Array.prototype.slice.call(arguments, 0);
}
function SubClass() {
super(Array.prototype.slice.call(arguments, 0));
}
SubClass.prototype = Object.create(BaseClass.prototype);
Question 4
Question
When does a Closure scope gets created?
Answer
Every function has a Closure scope associated with it
Every time a inner function is defined within another function
When an inner function is defined within another function and that refers to variables that are available at the defining function (local variables of enclosing functions and its parent right until global)
A closure keyword is available. When a variable is declared with that modifier, closure scope is created