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
- 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’);
- 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’);
- 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’);
- Both 1,2
- Both 1,3