1.
What does the below line do considering it as the first line in your javascript file
(function(){alert(this)})();
Correct Answer
E. I will see an alert with not null value
Explanation
In a function called directly without an explicit owner object, like myFunction(), causes the value of this to be the default object (window in the browser).
2.
-
<input type="button" value="Button 1" id="btn1" />
-
<input type="button" value="Button 2" id="btn2" />
-
<input type="button" value="Button 3" id="btn3" onclick="buttonClicked();"/>
-
-
<script type="text/javascript">
-
function buttonClicked(){
-
var text = (this === window) ? 'window' : this.id;
-
alert( text );
-
}
-
var button1 = document.getElementById('btn1');
-
var button2 = document.getElementById('btn2');
-
-
button1.onclick = buttonClicked;
-
button2.onclick = function(){ buttonClicked(); };
-
</script>
Correct Answer
D. Clicking on button 3 displays something else
Explanation
In a function called directly without an explicit owner object, like myFunction(), causes the value of this to be the default object (window in the browser). In a function called using the method invocation syntax, like obj.myFunction() or obj['myFunction'](), causes the value of this to be obj.
3.
Apply or call method can be used to override the value of this.
Correct Answer
A. True
Explanation
The statement is true because both the apply and call methods can be used to override the value of "this" in JavaScript. These methods allow you to explicitly set the value of "this" within a function, allowing you to control the context in which the function is executed. This can be useful when you want to invoke a function with a specific object as the value of "this", rather than the default value determined by how the function is called.
4.
When used as a constructor, like a new MyFunction(), the value of this will be a brand new object provided by the JavaScript runtime. If we don't explicitly return anything from that function, this will be considered its return value.
Correct Answer
A. True
Explanation
When a function is used as a constructor with the "new" keyword, it creates a new object. The value of "this" inside the constructor refers to this newly created object. If the constructor function does not have an explicit return statement, the newly created object will be considered as the return value. Therefore, the statement "When used as a constructor, like a new MyFunction(), the value of this will be a brand new object provided by the JavaScript runtime. If we don't explicitly return anything from that function, this will be considered its return value" is true.
5.
When you create a plain Object, JavaScript will automatically assign a Prototype to each of these objects. This prototype will be Object.prototype.
Correct Answer
A. True
Explanation
When creating a plain Object in JavaScript, a prototype is automatically assigned to each of these objects. This prototype is set to Object.prototype. Therefore, the statement "When you create a plain Object, JavaScript will automatically assign a Prototype to each of these objects. This prototype will be Object.prototype" is true.
6.
The prototype for "hi" is String.Prototype , and the prototype for String.Prototype is Object.Prototype
Correct Answer
A. True
Explanation
The statement is true because in JavaScript, every object has a prototype, which is an object from which it inherits properties and methods. The prototype for the string "hi" is String.prototype, which is the prototype object for all string objects. And the prototype for String.prototype is Object.prototype, which is the prototype object for all objects in JavaScript. Therefore, the prototype chain for "hi" goes from String.prototype to Object.prototype.
7.
Function Employee() {
}
Employee.ShowAge = function() {
alert("sss");
}
This adds a static method to the Employee function which would not be available to the objects of Employee function
Correct Answer
A. True
Explanation
This code adds a static method called ShowAge to the Employee function. Static methods are attached to the function itself, rather than the objects created from the function. Therefore, this method would not be available to the objects created from the Employee function. This statement is true.
8.
Employee.prototype.constructor would be Employee be default. This needs to be explicitly set in the following scenario otherwise it would be Employee for Manager.constructor.prototype
Manager.prototype = new Employee();
Manager.constructor.prototype = Manager;
Correct Answer
A. True
Explanation
In JavaScript, when an object is created using a constructor function, its prototype property is automatically set to the constructor's prototype object. In the given scenario, the Manager object is created using the Employee constructor, so its prototype property would be set to the Employee constructor's prototype object. Therefore, the statement "Manager.prototype = new Employee();" sets the prototype of Manager to the Employee constructor's prototype object. And the statement "Manager.constructor.prototype = Manager;" sets the constructor's prototype of Manager to itself. Hence, the correct answer is true.