Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Creating our own Emitter property //
- //////////////////////////////////////
- // if your using a core node property you don't need the ./ before the property
- // ./ is if you're lookign for somethign on the same file level
- // THIS FILE USES THE INTERNAL NODE EVENT EMITTER
- var Emitter = require('events');
- // events is the property of the config.js object you're looking for
- var eventListener = require('./config').events;
- var emtr = new Emitter();
- emtr.on(eventListener.GREET, function(){
- console.log('hellloooo okay? using the INTERNAL NODE EVENT EMITTER')
- });
- emtr.on(eventListener.GREET, function(){
- console.log('A greeeting occuredz');
- })
- // 'boom' is the triggerz
- emtr.emit(eventListener.GREET);
- // basically this is an event listener but modular, which you technically can't do in JS
- // 'events' is a listener.
- // 1) set require('events') to a variable
- // 2) make a new object with that constuctor
- // 3) create a trigger string with that new object
- // 4) to which you can call a function on
- // 5) best to set the string as an object because you may mistype the string and it'd be hard to debug
- // do that in a new file (config.js) or whatever
- //////////////////////////////////////
- // OR USING NODE'S EMITTER PROPERTY //
- /////////////////////////////////////
- // util & events are built in Node methods
- // events contains .emit and .on
- var EventEmitter = require('events');
- var util = require('util');
- function Greetr() {
- this.greeting = "Hello worldz!";
- }
- // sets up the prototype chain so that any objects created on Greetr will also find the prototype on EventEmitter
- // giving the Greetr object access to all the methods and props EventEmitter has access to, like .on and .emit
- util.inherits(Greetr, EventEmitter);
- Greetr.prototype.greet = function(data) {
- console.log(this.greeting + " " + data); // "Hello worldz!" + data from the function call at bottom
- // the magic string from the .on method below called from the prototype chain
- this.emit('greet', data);
- }
- // greeter1 has access to the props of both Greetr and EventEmitter
- var greeter1 = new Greetr();
- greeter1.on('greet', function() {
- console.log('Someone greeted, yo! ' + data);
- });
- greeter1.greet("I'm the data");
- //////////////////////////////////////
- // EMITTER PROPERTY WORKING EXAMPLE //
- /////////////////////////////////////
- var util = require('util');
- function Person() {
- this.firstname = 'John';
- this.lastname = 'Doe';
- }
- // Person.prototype has access to both Person & Policeman object Constuctors
- Person.prototype.greet = function() {
- console.log('Hello ' + this.firstname + ' ' + this.lastname + ' for badge ' + this.badgenumber);
- }
- function Policeman() {
- // this line is used to grab the properties of the Person object for Policeman
- // otherwise they'd be undefined when running proto.greet on new Policeman
- Person.call(this);
- this.badgenumber = '1234';
- }
- // says Policeman should inherit from Person
- // gives Policeman the properties of Person (i.e. .firstname, .lastname)
- // won't work without this b/c it means there is no new Policeman
- util.inherits(Policeman, Person);
- var officer = new Policeman();
- officer.greet();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement