What are the different ways in which you can create a javascript object? Choose ALL the right options
Respuesta
var object = { prop1: "value1", prop2 : "value2"};
var object = new Object();
var object = {};
var object = Object.create({});
Pregunta 2
Pregunta
How to create a constructor function?
Respuesta
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
Pregunta 3
Pregunta
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.
Respuesta
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);
Pregunta 4
Pregunta
When does a Closure scope gets created?
Respuesta
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