-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototipo.js
65 lines (43 loc) · 1.33 KB
/
prototipo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict';
// hacer función constructora
var persona = function(name){
this.name = name;
};
// crear una instancia
var luis = new persona('Luis');
console.log(luis.name);
// cambiar su prototipo
persona.prototype.saluda = function(){
console.log(this.name + ' te saluda!');
};
luis.saluda();
var pepe = new persona('Pepe');
pepe.saluda();
////////// HERENCIA
// otra función constructora que hereda de persona, formato para usar new
var Agente = function(name) {
// ejecutamos constructor heredado (llamar a super)
persona.call(this, name);
};
Agente.prototype = new persona('prototipo');
// creamos un nuevo Agente
var smith = new Agente('Smith');
// comprobamos que tiene los métodos de persona
smith.saluda();
var extend = require('util')._extend;
var EventEmitter = require('events').EventEmitter;
//console.log(EventEmitter);
Agente.prototype = extend(Agente.prototype, EventEmitter.prototype);
var jose = new Agente('Jose');
//console.log(Object.getPrototypeOf(jose));
jose.on('llamada de telefono', function(data) {
console.log('brr brr', data);
});
jose.emit('llamada de telefono', {payload: 2000});
var terminator = new Agente('terminator');
terminator.on('fin de programa', function(data) {
console.log('Termina el programa', data);
});
process.on('exit', function() {
jose.emit('fin de programa','Se acaba');
});