When a function returns a new object each time it is called, even with the same inputs, is it considered a pure function?
- Yes
- No
- Depends on the type of object
- Only if the object is immutable
A pure function should consistently return the same result for the same inputs, and creating new objects introduces unpredictability, making it impure.
In ES6, __________ methods allow for concise method definitions without the function keyword.
- Arrow
- Define
- Class
- Prototype
In ES6, arrow functions provide a concise syntax for defining functions. The arrow (=>) is used to declare arrow functions, and they are particularly useful for defining methods on objects without the need for the function keyword. Arrow functions inherit the this value from the enclosing scope, making them convenient for defining methods within ES6 classes.
In what scenarios is it not recommended to use arrow functions in JavaScript?
- In event handlers
- In methods of classes
- In functions with dynamic this
- In callback functions within a setTimeout
Arrow functions do not have their own 'this' context and inherit it from the enclosing scope. This can lead to unexpected behavior in functions that rely on the dynamic binding of 'this'. It is not recommended to use arrow functions in functions with dynamic 'this' requirements.
Is it possible to call a static method from another static method within the same class?
- No, it leads to a syntax error.
- Yes, using the class name.
- No, static methods cannot call each other.
- Yes, using the this keyword.
Yes, it is possible to call a static method from another static method within the same class using the class name. Static methods are associated with the class and can be called using the class name to avoid confusion. Option B is correct.
Which statement correctly initiates a dynamic import in ES6?
- import module from 'module'
- require('module')
- import('module')
- require.ensure(['module'], callback)
The correct syntax for initiating a dynamic import in ES6 is import('module'). This allows you to asynchronously load and work with modules at runtime, enhancing flexibility in the application structure.
Tree shaking can fail to eliminate unused code if modules are dynamically _______ at runtime.
- Imported
- Exported
- Loaded
- Unloaded
The correct option is (c) Loaded. Tree shaking may fail if modules are dynamically loaded at runtime. Dynamically loading modules introduces uncertainty about which modules will be used, making it challenging for the module bundler to perform effective tree shaking.
To rename a named export during import, use the syntax import { originalName as ______ } from 'module-name';.
- newName
- importedName
- aliasName
- renamedName
When importing a named export and giving it a new name, use the as keyword, making option c) aliasName the correct choice. The syntax is import { originalName as aliasName } from 'module-name';.
Can JavaScript handle asynchronous tasks natively, and if so, how is this achieved in relation to the event loop?
- Yes, using callbacks.
- Yes, through asynchronous functions and promises.
- No, JavaScript cannot handle asynchronous tasks natively.
- Yes, through synchronous functions.
JavaScript can handle asynchronous tasks through asynchronous functions and promises. The event loop ensures that asynchronous code is executed at the appropriate time.
Consider a scenario where you have a base class for a UI component and multiple derived classes for specific components. How would the constructors and super keyword play a role in initializing state and props?
- Constructors are unnecessary; use default values for state and props.
- Use constructors for state and props initialization without involving the super keyword.
- Employ both constructors and the super keyword to initialize state and props in a structured manner.
- Rely on setter methods to initialize state and props in UI components.
In a scenario with a base UI component class and derived classes, constructors play a crucial role. Utilizing both constructors and the super keyword ensures a systematic approach to initializing state and props, fostering a clear and organized class hierarchy for UI components.
When implementing code splitting using dynamic imports, large bundles are broken into smaller __________.
- Components
- Chunks
- Fragments
- Segments
Code splitting is the technique of breaking large bundles into smaller chunks, and in ES6, this is achieved through dynamic imports, creating separate chunks. Code chunks are often referred to as "chunks" in this context.