How can a method be added to an object's prototype?
- object.prototype.addMethod(methodName, method)
- prototype.addMethod(object, methodName, method)
- Object.prototype.methodName = method
- object.methodName = method
To add a method to an object's prototype in JavaScript, you can assign the method to the prototype property of the constructor function that created the objects. For example, Object.prototype.methodName = method. This way, all objects created from that constructor will inherit the method. The other options do not follow the correct syntax for adding a method to a prototype.
Loading...
Related Quiz
- Which of the following is a primary feature of a testing framework like Mocha or Jest?
- You are tasked with refactoring a large codebase to make it more modular. What considerations should you have regarding the import and export of modules to ensure smooth transition and maintainability?
- You are working on optimizing a web application that has a high First Contentful Paint (FCP) time. Which strategies would be effective in reducing the FCP time without compromising the functionality of the application?
- Which of the following is true regarding the 'let' keyword in ES6+?
- In JavaScript, variables declared using the var keyword are hoisted to the top of their ________.