To inherit properties and methods from another class in ES6, use the ________ keyword.
- Inherits
- Extends
- InheritsFrom
- ExtendsFrom
In ES6, the extends keyword is used to inherit properties and methods from another class.
How does the syntax of an arrow function differ from a traditional function in JavaScript?
- Uses a shorter syntax (=>)
- Uses the function keyword
- Uses a different declaration keyword
- No difference in syntax
Arrow functions in ES6 use a shorter syntax (=>) compared to the traditional function declaration. This concise syntax is especially useful for simple one-liner functions.
In ES6, setting "type": "______" in package.json informs Node.js to treat JavaScript files as ES modules.
- "ES6"
- "module"
- "script"
- "import"
By setting "type": "module" in package.json, Node.js is informed to treat JavaScript files as ES modules, allowing the use of import/export syntax and adhering to ES6 module behavior.
Question 2: In a large-scale application, how does the use of absolute imports in ES6 modules affect maintainability and refactoring compared to relative imports?
- Easier refactoring, better maintainability
- No significant impact
- Harder refactoring, lower maintainability
- Depends on the project structure
Using absolute imports can make refactoring more challenging, as changes to the file structure may require updating import paths throughout the codebase. This can result in lower maintainability compared to relative imports, which automatically adjust to the file's position. Consider the trade-offs based on project size and structure.
In an AJAX call using Promises, _________ is a method used to parse the JSON response.
- .parseJSON()
- .toJSON()
- .parse()
- .json()
The correct method for parsing the JSON response in a Promises-based AJAX call is .json(). This method is specifically designed to extract and parse the JSON data from the response.
How can you import an entire module as an object in ES6?
- import * as myModule from './myModule';
- import { myModule } from './myModule';
- import myModule from './myModule';
- import { * } from './myModule';
In ES6, you can use the import * as alias syntax to import the entire module as an object. This allows you to access all exported values through the alias object.
In what scenario would a WeakSet be more appropriate than a Set for managing DOM elements?
- When managing temporary or transient DOM elements that should not prevent garbage collection.
- WeakSet is preferred for static and permanent DOM elements to enhance performance.
- In scenarios where strict ordering of DOM elements is crucial.
- When managing a small number of DOM elements with known lifetimes.
WeakSet is suitable for scenarios where the collection of DOM elements may vary, and automatic garbage collection is desired.
What happens when you try to create a new Symbol using the new keyword?
- Error is thrown
- A new Symbol is created
- It returns an existing Symbol
- Symbols cannot be created using new
When attempting to create a Symbol using the new keyword, it results in an error. Symbols are primitive values, and using the new keyword with them is not allowed. Symbols are typically created without the new keyword.
The _______ operator can be used to unpack the values from an iterable into individual elements.
- spread
- merge
- combine
- join
The spread operator (...) in JavaScript is used to unpack elements from an iterable, such as an array, and spread them into individual elements or combine them with other elements.
How does static analysis contribute to tree shaking in ES6 modules?
- It dynamically analyzes the code at runtime to identify unused modules
- It performs analysis at compile-time to identify and eliminate unused exports
- It relies on dynamic imports to load only necessary modules
- It statically enforces tree shaking by default in ES6
Static analysis in tree shaking occurs during the compilation phase. It identifies and eliminates unused exports by analyzing the code without executing it. This helps in reducing the bundle size by excluding unnecessary code paths and dependencies.
Which tool is essential for enabling tree shaking in a modern JavaScript project?
- Webpack
- Babel
- ESLint
- npm
Tree shaking is a process in Webpack that eliminates unused code, reducing the bundle size. By configuring Webpack properly, you can enable tree shaking and optimize your project for better performance.
A class in ES6 is a special type of function, and it can be declared using the ________ keyword.
- function
- class
- object
- define
In ES6, the class keyword is used to declare a class. Unlike functions, classes support class-based inheritance, providing a cleaner syntax for object-oriented programming in JavaScript.