Skip to content

Commit 7293d73

Browse files
committed
update prototype model
1 parent 17d83cb commit 7293d73

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

02-languages/02-apuntes/01-javascript/201 prototype model.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ dan.age = 26;
6767
console.log(dan.age); // 26
6868
console.log(james.age); // undefined
6969

70-
// También podemos crear estas propiedades directamente en el "constructor".
71-
// Podemos crear funciones por ejemplo:
70+
// Genial, pues vamos a crear métodos, por ejemplo:
7271
function Person(name) {
7372
this.name = name;
7473
this.greet = function () {
@@ -186,12 +185,15 @@ function Taxi() {
186185
// Hacemos que Taxi "herede" de Automobile. Para ello creamos un prototipo para Taxi, gracias a
187186
// Object.create que crea un nuevo objeto cuyo prototipo podemos hacer que apunte a donde queramos.
188187
// Ademas hay que setear su constructor.
189-
Taxi.prototype = Object.create(Automobile.prototype); // Crea un objeto nuevo {__proto__: arg}
190-
Taxi.prototype.constructor = Taxi;
188+
189+
Object.setPrototypeOf(Taxi.prototype, Automobile.prototype);
190+
191191
// Una forma equivalente sería directamente apuntando a donde queramos
192+
// Taxi.prototype = Object.create(Automobile.prototype); // Crea un objeto nuevo {__proto__: arg}
193+
// Taxi.prototype.constructor = Taxi;
194+
// Incluso también podríamos hacerlo de la siguiente forma:
192195
// Taxi.prototype.__proto__ = Automobile.prototype;
193-
// Otra forma más limpia sin sobreescribir esta propiedad __proto__ es:
194-
// Object.setPrototypeOf(Taxi.prototype, Automobile.prototype);
196+
// Taxi.prototype.constructor = Taxi;
195197

196198
// Añadimos también algún método a Taxi.
197199
Taxi.prototype.service = function () {

0 commit comments

Comments
 (0)