Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. // Creating our own Emitter property //
  2. //////////////////////////////////////
  3. // if your using a core node property you don't need the ./ before the property
  4. // ./ is if you're lookign for somethign on the same file level
  5. // THIS FILE USES THE INTERNAL NODE EVENT EMITTER
  6. var Emitter = require('events');
  7. // events is the property of the config.js object you're looking for
  8. var eventListener = require('./config').events;
  9.  
  10. var emtr = new Emitter();
  11.  
  12. emtr.on(eventListener.GREET, function(){
  13. console.log('hellloooo okay? using the INTERNAL NODE EVENT EMITTER')
  14. });
  15.  
  16. emtr.on(eventListener.GREET, function(){
  17. console.log('A greeeting occuredz');
  18. })
  19.  
  20. // 'boom' is the triggerz
  21. emtr.emit(eventListener.GREET);
  22.  
  23. // basically this is an event listener but modular, which you technically can't do in JS
  24. // 'events' is a listener.
  25. // 1) set require('events') to a variable
  26. // 2) make a new object with that constuctor
  27. // 3) create a trigger string with that new object
  28. // 4) to which you can call a function on
  29.  
  30. // 5) best to set the string as an object because you may mistype the string and it'd be hard to debug
  31. // do that in a new file (config.js) or whatever
  32.  
  33. //////////////////////////////////////
  34. // OR USING NODE'S EMITTER PROPERTY //
  35. /////////////////////////////////////
  36. // util & events are built in Node methods
  37. // events contains .emit and .on
  38. var EventEmitter = require('events');
  39. var util = require('util');
  40.  
  41. function Greetr() {
  42. this.greeting = "Hello worldz!";
  43. }
  44.  
  45. // sets up the prototype chain so that any objects created on Greetr will also find the prototype on EventEmitter
  46. // giving the Greetr object access to all the methods and props EventEmitter has access to, like .on and .emit
  47. util.inherits(Greetr, EventEmitter);
  48.  
  49. Greetr.prototype.greet = function(data) {
  50. console.log(this.greeting + " " + data); // "Hello worldz!" + data from the function call at bottom
  51. // the magic string from the .on method below called from the prototype chain
  52. this.emit('greet', data);
  53. }
  54.  
  55. // greeter1 has access to the props of both Greetr and EventEmitter
  56. var greeter1 = new Greetr();
  57.  
  58. greeter1.on('greet', function() {
  59. console.log('Someone greeted, yo! ' + data);
  60. });
  61. greeter1.greet("I'm the data");
  62.  
  63.  
  64.  
  65. //////////////////////////////////////
  66. // EMITTER PROPERTY WORKING EXAMPLE //
  67. /////////////////////////////////////
  68. var util = require('util');
  69.  
  70. function Person() {
  71. this.firstname = 'John';
  72. this.lastname = 'Doe';
  73. }
  74.  
  75. // Person.prototype has access to both Person & Policeman object Constuctors
  76. Person.prototype.greet = function() {
  77. console.log('Hello ' + this.firstname + ' ' + this.lastname + ' for badge ' + this.badgenumber);
  78. }
  79.  
  80. function Policeman() {
  81. // this line is used to grab the properties of the Person object for Policeman
  82. // otherwise they'd be undefined when running proto.greet on new Policeman
  83. Person.call(this);
  84. this.badgenumber = '1234';
  85. }
  86.  
  87. // says Policeman should inherit from Person
  88. // gives Policeman the properties of Person (i.e. .firstname, .lastname)
  89. // won't work without this b/c it means there is no new Policeman
  90. util.inherits(Policeman, Person);
  91. var officer = new Policeman();
  92. officer.greet();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement