Skip to content

Commit

Permalink
lib: reduce usage of self = this
Browse files Browse the repository at this point in the history
Remove unnecessary `self = this`.

PR-URL: #5231
Reviewed-By: James M Snell <[email protected]>

Conflicts:
	lib/domain.js
  • Loading branch information
JacksonTian authored and Fishrock123 committed Mar 22, 2016
1 parent e20d0b8 commit c75f97f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 43 deletions.
5 changes: 2 additions & 3 deletions doc/api/stream.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -1026,15 +1026,14 @@ function SimpleProtocol(source, options) {
// source is a readable stream, such as a socket or file
this._source = source;

var self = this;
source.on('end', () => {
self.push(null);
this.push(null);
});

// give it a kick whenever the source is readable
// read(0) will not consume any bytes
source.on('readable', () => {
self.read(0);
this.read(0);
});

this._rawHeader = [];
Expand Down
3 changes: 1 addition & 2 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,7 @@ Socket.prototype.close = function(callback) {
this._stopReceiving();
this._handle.close();
this._handle = null;
var self = this;
process.nextTick(socketCloseNT, self);
process.nextTick(socketCloseNT, this);

return this;
};
Expand Down
2 changes: 1 addition & 1 deletion lib/domain.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Domain.prototype._errorHandler = function errorHandler(er) {
// as this would throw an error, make the process exit, and thus
// prevent the process 'uncaughtException' event from being emitted
// if a listener is set.
if (EventEmitter.listenerCount(self, 'error') > 0) {
if (EventEmitter.listenerCount(this, 'error') > 0) {
try {
// Set the _emittingTopLevelDomainError so that we know that, even
// if technically the top-level domain is still active, it would
Expand Down
5 changes: 2 additions & 3 deletions lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,11 @@ function createConnection(port, host, options) {
}
}

const self = this;
const socket = tls.connect(options, function() {
const socket = tls.connect(options, () => {
if (!options._agentKey)
return;

self._cacheSession(options._agentKey, socket.getSession());
this._cacheSession(options._agentKey, socket.getSession());
});

// Evict session on error
Expand Down
47 changes: 21 additions & 26 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,8 @@ Socket.prototype.read = function(n) {

Socket.prototype.listen = function() {
debug('socket.listen');
var self = this;
self.on('connection', arguments[0]);
listen(self, null, null, null);
this.on('connection', arguments[0]);
listen(this, null, null, null);
};


Expand Down Expand Up @@ -883,7 +882,6 @@ Socket.prototype.connect = function(options, cb) {
this._sockname = null;
}

var self = this;
var pipe = !!options.path;
debug('pipe', pipe, options.path);

Expand All @@ -893,21 +891,20 @@ Socket.prototype.connect = function(options, cb) {
}

if (typeof cb === 'function') {
self.once('connect', cb);
this.once('connect', cb);
}

this._unrefTimer();

self._connecting = true;
self.writable = true;
this._connecting = true;
this.writable = true;

if (pipe) {
connect(self, options.path);

connect(this, options.path);
} else {
lookupAndConnect(self, options);
lookupAndConnect(this, options);
}
return self;
return this;
};


Expand Down Expand Up @@ -1194,11 +1191,10 @@ exports._createServerHandle = createServerHandle;

Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
debug('listen2', address, port, addressType, backlog, fd);
var self = this;

// If there is not yet a handle, we need to create one and bind.
// In the case of a server sent via IPC, we don't need to do this.
if (self._handle) {
if (this._handle) {
debug('_listen2: have a handle already');
} else {
debug('_listen2: create a handle');
Expand All @@ -1223,22 +1219,22 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {

if (typeof rval === 'number') {
var error = exceptionWithHostPort(rval, 'listen', address, port);
process.nextTick(emitErrorNT, self, error);
process.nextTick(emitErrorNT, this, error);
return;
}
self._handle = rval;
this._handle = rval;
}

self._handle.onconnection = onconnection;
self._handle.owner = self;
this._handle.onconnection = onconnection;
this._handle.owner = this;

var err = _listen(self._handle, backlog);
var err = _listen(this._handle, backlog);

if (err) {
var ex = exceptionWithHostPort(err, 'listen', address, port);
self._handle.close();
self._handle = null;
process.nextTick(emitErrorNT, self, ex);
this._handle.close();
this._handle = null;
process.nextTick(emitErrorNT, this, ex);
return;
}

Expand All @@ -1249,7 +1245,7 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
if (this._unref)
this.unref();

process.nextTick(emitListeningNT, self);
process.nextTick(emitListeningNT, this);
};


Expand Down Expand Up @@ -1517,15 +1513,14 @@ Server.prototype.close = function(cb) {

Server.prototype._emitCloseIfDrained = function() {
debug('SERVER _emitCloseIfDrained');
var self = this;

if (self._handle || self._connections) {
if (this._handle || this._connections) {
debug('SERVER handle? %j connections? %d',
!!self._handle, self._connections);
!!this._handle, this._connections);
return;
}

process.nextTick(emitCloseNT, self);
process.nextTick(emitCloseNT, this);
};


Expand Down
6 changes: 3 additions & 3 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@ function regexpEscape(s) {
REPLServer.prototype.convertToContext = function(cmd) {
const scopeVar = /^\s*var\s*([_\w\$]+)(.*)$/m;
const scopeFunc = /^\s*function\s*([_\w\$]+)/;
var self = this, matches;
var matches;

// Replaces: var foo = "bar"; with: self.context.foo = bar;
matches = scopeVar.exec(cmd);
Expand All @@ -1133,9 +1133,9 @@ REPLServer.prototype.convertToContext = function(cmd) {
}

// Replaces: function foo() {}; with: foo = function foo() {};
matches = scopeFunc.exec(self.bufferedCommand);
matches = scopeFunc.exec(this.bufferedCommand);
if (matches && matches.length === 2) {
return matches[1] + ' = ' + self.bufferedCommand;
return matches[1] + ' = ' + this.bufferedCommand;
}

return cmd;
Expand Down
9 changes: 4 additions & 5 deletions test/parallel/test-zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,10 @@ SlowStream.prototype.resume = function() {

SlowStream.prototype.end = function(chunk) {
// walk over the chunk in blocks.
var self = this;
self.chunk = chunk;
self.length = chunk.length;
self.resume();
return self.ended;
this.chunk = chunk;
this.length = chunk.length;
this.resume();
return this.ended;
};


Expand Down

0 comments on commit c75f97f

Please sign in to comment.