¿Qué es exactamente el 'constructor' en lo siguiente? Es decir, ¿qué hace Object.create en lugar de llamar al constructor? ¿El constructor realmente ejecuta el nombre de la función, o qué magia sucede allí?
function greet(name) { this.print = function print() {console.log('hey!', name)}; } // why's it required to create an object of the funcion.prototype and again initialize the constructor? let y = Object.create(greet.prototype); y.constructor('Bob'); y.print(); // this part makes sense to me. let z = new greet('Bob'); z.print(); Además, si tuviera que poner el método print() fuera de la función, ¿cómo accedería al nombre? Por ejemplo:
function greet(name) { this.name = name; } greet.prototype.print = () => {console.log('hey!', this.name)}; // 'hey! undefined'