In JavaScript, a common way to utilize prototypes is by assigning a(n) _________ to an object's prototype property.
- Array
- Object Literal
- Constructor Function
- Prototype Object
In JavaScript, prototypes are often utilized by assigning a constructor function to an object's prototype property. This constructor function serves as a blueprint for creating objects with shared properties and methods. The prototype object associated with this constructor contains those shared properties and methods.
In Internet Explorer, instead of addEventListener, the _________ method is used to attach event listeners.
- attachEvent()
- registerListener()
- listenForEvent()
- addListener()
In Internet Explorer, the attachEvent() method is used to attach event listeners. This method is specific to Internet Explorer and serves a similar purpose to addEventListener() in other browsers, allowing you to respond to events such as clicks and keypresses.
If you’re using arrow functions to define methods inside a class, those methods will not have access to the class’s instance without using _________.
- super()
- this
- prototype
- new
If you use arrow functions to define methods within a class, they will not have their own this context. Instead, they inherit the this context from the surrounding scope. To access the class instance within an arrow function, you need to use this. Hence, the correct option is this.
How can you prematurely terminate a while loop?
- break statement
- return statement
- continue statement
- exit statement
You can prematurely terminate a while loop using the break statement. When a break statement is encountered within the loop, it exits the loop prematurely, regardless of whether the loop's condition is still true. This is useful for stopping the loop when a specific condition is met.
It’s possible to create a switch-like behavior using object literals and the _________ method in JavaScript.
- forEach()
- map()
- reduce()
- hasOwnProperty()
It’s possible to create a switch-like behavior using object literals and the hasOwnProperty() method in JavaScript. By defining properties in an object literal and then checking if a specific property exists using hasOwnProperty(), you can achieve similar branching behavior as a switch statement.
The property event.target gives access to the _________ that triggered the event.
- element
- object
- source
- target
The event.target property in JavaScript gives access to the element that triggered the event. It's especially useful in event delegation and can be used to identify which specific element was interacted with in a group of similar elements.
What happens when a function declaration and a var variable are hoisted in the same scope?
- The var variable takes precedence and shadows the function declaration.
- The function declaration takes precedence and shadows the var variable.
- JavaScript throws an error since function declarations and var variables can't be hoisted in the same scope.
- They both coexist in the same scope, and there's no shadowing or precedence.
When a function declaration and a var variable with the same name are hoisted in the same scope, the function declaration takes precedence and shadows the var variable. This is known as "hoisting," and it means that the function is accessible throughout the scope, even before its actual declaration in the code.
The ________ method is used to parse a JSON string into a JavaScript object.
- toJSON()
- parseJSON()
- JSON.parse()
- stringToJSON()
The correct method to parse a JSON string into a JavaScript object is JSON.parse(). It takes a JSON string as input and returns a JavaScript object. The other options (toJSON(), parseJSON(), stringToJSON()) are not valid JSON parsing methods.
What is the primary use of the switch statement in JavaScript?
- To declare variables
- To create loops
- To perform conditional branching
- To define functions
The primary use of the switch statement in JavaScript is to perform conditional branching. It allows you to execute different code blocks based on the value of an expression, making it a powerful tool for decision-making in your code.
How can a subclass be created in JavaScript?
- Using the extends keyword
- By defining a new class with the same name
- Using the super keyword
- By using the inherit keyword
In JavaScript, you can create a subclass by using the extends keyword. This allows you to inherit the properties and methods of another class, forming a class hierarchy. The extends keyword establishes a relationship between the subclass and the superclass.