Why can't I assign the prototype to c
, like this so that it contains the methods of the class Array
c.prototype = Object.create(Array.prototype); // Aquí lo asigno
window.addEventListener('DOMContentLoaded',() => {
var c = document.getElementsByClassName('clases');
c.prototype = Object.create(Array.prototype);
c.forEach(function(clase){
console.log(clase.className);
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<a class="clases">Enlace 1</a>
<a class="clases">Enlace 2</a>
<a class="clases">Enlace 3</a>
<a class="clases">Enlace 4</a>
</body>
</html>
Because with
what you do is create an object that has the same prototype as the class
Array
. therefore withyou have an object
c
that has an attributeprototype
an object that has as its prototype the prototype ofArray
.Also think that when you create an instance of a class, this instance has the prototype assigned to
<nombre_instancia>.__proto__
it and not to<nombre_instancia>.prototype
.You could actually do something like the following and it would be easier:
To consume the methods in the
prototype
, you have to initialize the object and the objectc
has already been initialized. You cannot modify theprototype
of an object at runtime unless you create a new instance of it:If you want to "inherit" from some other object, you have to specify it in its definition: