Using the new keyword invokes a _______ that creates a new object.
- constructor
- function
- class
- prototype
Using the new keyword in JavaScript invokes a constructor function that creates a new object. This object is then initialized with properties and methods defined within the constructor function. The constructor function serves as a blueprint for creating objects.
How can Cross-Origin Resource Sharing (CORS) issues be handled when using AJAX?
- Disable CORS in the browser
- Use JSONP for requests
- Send requests with credentials
- Use the 'Access-Control-Allow-Origin' header
Cross-Origin Resource Sharing (CORS) issues when using AJAX can be handled by configuring the server to include the 'Access-Control-Allow-Origin' header in its response. This header specifies which origins are allowed to access the server's resources. You can set it to '*' to allow any origin or specify specific origins. Additionally, if you need to include credentials (e.g., cookies) in your requests, you should set the 'withCredentials' property to true in your AJAX request.
You're developing a game and need to create multiple instances of a player object, each with slightly different properties. Which object creation pattern might be most appropriate to use?
- Prototype Pattern
- Singleton Pattern
- Factory Method Pattern
- Abstract Factory Pattern
In game development, you would often use the Factory Method Pattern. This pattern provides an interface for creating objects but allows subclasses to alter the type of objects that will be created. In the context of a game, you can have a PlayerFactory that creates different types of players with varying properties while maintaining a consistent creation interface.
How does the “for...of” loop differ from the traditional "for" loop?
- It cannot iterate over arrays.
- It can only be used for asynchronous operations.
- It is specifically designed for iterating over arrays and iterable objects.
- It has better performance when iterating over objects.
The "for...of" loop is used for iterating over arrays and iterable objects, providing a simplified syntax compared to the traditional "for" loop. It's particularly useful when dealing with collections of data, offering cleaner code for iteration. Traditional "for" loops are more versatile but require more verbose code for array iteration.
What is "explicit binding" in JavaScript with regard to the "this" keyword?
- Explicit binding refers to manually setting the "this" value for a function using methods like .bind(), .call(), or .apply().
- Explicit binding is when "this" is automatically determined by the JavaScript engine.
- Explicit binding involves using the "this" keyword without any special methods.
- Explicit binding is only applicable to arrow functions.
In JavaScript, "explicit binding" refers to the manual setting of the "this" value for a function. This is done using methods like .bind(), .call(), or .apply(), allowing you to explicitly specify the object that should be treated as "this" within the function. This is especially useful in cases where the default "this" behavior is not what you need.
What does the responseType property of an XMLHttpRequest object determine?
- The data type of the request
- The request method
- The URL of the request
- The timeout for the request
The responseType property of an XMLHttpRequest object determines the expected data type of the response. It allows you to specify whether you expect the response to be in text, JSON, XML, or other formats. Setting this property correctly is important for parsing and handling the response data properly in your JavaScript code.
In JavaScript, instances are typically created using the _________ keyword.
- class
- new
- instance
- create
In JavaScript, instances of objects are typically created using the new keyword. For example, you can create a new instance of a constructor function using new ConstructorName(). This keyword is essential for creating object instances.
Which method should be used to handle exceptions in an async/await function?
- try...catch
- if...else
- switch...case
- throw...catch
To handle exceptions in an async/await function, you should use the 'try...catch' statement. This allows you to wrap the awaited code in a 'try' block and catch any exceptions that may occur within the 'catch' block. Using 'try...catch' ensures that your program can gracefully handle errors and prevent them from crashing your application.
Which of the following is NOT a type of polymorphism supported by JavaScript?
- Compile-time polymorphism
- Runtime polymorphism
- Ad-hoc polymorphism
- Parametric polymorphism
JavaScript supports runtime polymorphism and ad-hoc polymorphism (also known as function overloading), allowing different behavior for functions with the same name but different parameters. Parametric polymorphism is associated with generics and is not a form of polymorphism in JavaScript.
The _________ property of the XMLHttpRequest object holds the status of the XMLHttpRequest.
- statusText
- response
- status
- readyState
The status property of the XMLHttpRequest object holds the HTTP status code of the response received from the server. It indicates whether the request was successful or encountered an error. statusText contains the status message associated with the status code. response contains the response data.