Counter problem

You are writing a program which intends to increment the “hitCount” property of the “Analytics” object every time the http server gets a request.

Which of the following code segment(s) will work as intended?

Options
  1. var Analytics = { hitCount: 10, gotHit: function() { this.hitCount++; } } var http = require(‘http’); var server = http.createServer(function(req, res) { // process the request }); server.on(‘connection’, Analytics.gotHit.bind(Analytics)); server.listen(80, ‘0.0.0.0’);
  2. var Analytics = { hitCount: 10, gotHit: function() { this.hitCount++; } } var http = require(‘http’); var server = http.createServer(function(req, res) { Analytics.gotHit(); // process the request }); server.listen(80, ‘0.0.0.0’);
  3. var Analytics = { hitCount: 10, gotHit: function() { this.hitCount++; } } var http = require(‘http’); var server = http.createServer(function(req, res) { // process the request }); server.on(‘connection’, Analytics.gotHit); server.listen(80, ‘0.0.0.0’);
  4. Both 1,2
  5. Both 1,3

Related Posts