Skip to main content

Native Prototypes

Prototypes / Inheritance: Native Prototypes


Can you give me a high-level overview of JavaScript's native prototypes and objects?

View Answer:
Interview Response: The prototype property is present in all custom and built-in native objects, and we can improve their usefulness by adding additional attributes and methods. Native prototypes may only be changed or new ones added, but we cannot remove them.

What future issues can happen with native prototype extensions (custom property) in web applications?

View Answer:
Interview Response: If future browser versions implement Array.prototype.myExtension, their implementation gets overwritten by our extended method, which will not only be less efficient but may also produce a different, nonstandard result. Whether internal or external, conflicts between libraries are another issue that emerges.

Technical Response: If future browser versions implement Array.prototype.myExtension (either as part of an upgrade to the EcmaScript standard or on their initiative), their implementation gets overridden by the custom one, which will not only be less productive (we can't change browser engine internals in the service of method optimization), but may also produce a different, nonstandard result. However, there is a technique to reduce the danger by testing for the presence of the native property. This choice might result in varying results between browser versions and device platforms. Whether internal or external, conflicts between libraries are another issue that emerges.

note

Although, there is a way to mitigate the risk by checking for the existence of native properties. This behavior could lead to different results in different browsers versions and across device platforms.


What is a specific case for native prototype extensions in JavaScript?

View Answer:
Interview Response: You should only use a native prototype extension when you need to create a Polyfill for a method that exists in the JavaScript standard but is not yet supported by a particular JavaScript engine.

Code Example:

if (!String.prototype.repeat) {
// if there's no such method
// add it to the prototype

String.prototype.repeat = function (n) {
// repeat the string n times

// actually, the code should be a little bit more complex than that
// (the full algorithm is in the specification)
// but even an imperfect polyfill is often considered good enough
return new Array(n + 1).join(this);
};
}

alert('La'.repeat(3)); // LaLaLa

Can you borrow functionality from native prototypes in JavaScript?

View Answer:
Interview Response: Yes, you may borrow a native prototype method if you require the same functionality. The basic concept is to copy and paste a method from one object into another. We should note that some native prototype approaches often get copied.

Code Example: Borrowing the Array Join method…

let obj = {
0: 'Hello',
1: 'JavaScript!',
length: 2,
};

obj.join = Array.prototype.join;

alert(obj.join(', ')); // Hello, JavaScript!