Skip to content

Commit 108fc90

Browse files
JacksonTianMyles Borins
authored and
Myles Borins
committed
lib: reduce usage of self = this
Remove unnecessary `self = this`. PR-URL: #5231 Reviewed-By: James M Snell <[email protected]>
1 parent 07b7172 commit 108fc90

File tree

6 files changed

+33
-42
lines changed

6 files changed

+33
-42
lines changed

doc/api/stream.markdown

+2-3
Original file line numberDiff line numberDiff line change
@@ -1026,15 +1026,14 @@ function SimpleProtocol(source, options) {
10261026
// source is a readable stream, such as a socket or file
10271027
this._source = source;
10281028

1029-
var self = this;
10301029
source.on('end', () => {
1031-
self.push(null);
1030+
this.push(null);
10321031
});
10331032

10341033
// give it a kick whenever the source is readable
10351034
// read(0) will not consume any bytes
10361035
source.on('readable', () => {
1037-
self.read(0);
1036+
this.read(0);
10381037
});
10391038

10401039
this._rawHeader = [];

lib/dgram.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,7 @@ Socket.prototype.close = function(callback) {
355355
this._stopReceiving();
356356
this._handle.close();
357357
this._handle = null;
358-
var self = this;
359-
process.nextTick(socketCloseNT, self);
358+
process.nextTick(socketCloseNT, this);
360359

361360
return this;
362361
};

lib/https.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,11 @@ function createConnection(port, host, options) {
6969
}
7070
}
7171

72-
const self = this;
73-
const socket = tls.connect(options, function() {
72+
const socket = tls.connect(options, () => {
7473
if (!options._agentKey)
7574
return;
7675

77-
self._cacheSession(options._agentKey, socket.getSession());
76+
this._cacheSession(options._agentKey, socket.getSession());
7877
});
7978

8079
// Evict session on error

lib/net.js

+21-26
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,8 @@ Socket.prototype.read = function(n) {
299299

300300
Socket.prototype.listen = function() {
301301
debug('socket.listen');
302-
var self = this;
303-
self.on('connection', arguments[0]);
304-
listen(self, null, null, null);
302+
this.on('connection', arguments[0]);
303+
listen(this, null, null, null);
305304
};
306305

307306

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

886-
var self = this;
887885
var pipe = !!options.path;
888886
debug('pipe', pipe, options.path);
889887

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

895893
if (typeof cb === 'function') {
896-
self.once('connect', cb);
894+
this.once('connect', cb);
897895
}
898896

899897
this._unrefTimer();
900898

901-
self._connecting = true;
902-
self.writable = true;
899+
this._connecting = true;
900+
this.writable = true;
903901

904902
if (pipe) {
905-
connect(self, options.path);
906-
903+
connect(this, options.path);
907904
} else {
908-
lookupAndConnect(self, options);
905+
lookupAndConnect(this, options);
909906
}
910-
return self;
907+
return this;
911908
};
912909

913910

@@ -1194,11 +1191,10 @@ exports._createServerHandle = createServerHandle;
11941191

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

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

12241220
if (typeof rval === 'number') {
12251221
var error = exceptionWithHostPort(rval, 'listen', address, port);
1226-
process.nextTick(emitErrorNT, self, error);
1222+
process.nextTick(emitErrorNT, this, error);
12271223
return;
12281224
}
1229-
self._handle = rval;
1225+
this._handle = rval;
12301226
}
12311227

1232-
self._handle.onconnection = onconnection;
1233-
self._handle.owner = self;
1228+
this._handle.onconnection = onconnection;
1229+
this._handle.owner = this;
12341230

1235-
var err = _listen(self._handle, backlog);
1231+
var err = _listen(this._handle, backlog);
12361232

12371233
if (err) {
12381234
var ex = exceptionWithHostPort(err, 'listen', address, port);
1239-
self._handle.close();
1240-
self._handle = null;
1241-
process.nextTick(emitErrorNT, self, ex);
1235+
this._handle.close();
1236+
this._handle = null;
1237+
process.nextTick(emitErrorNT, this, ex);
12421238
return;
12431239
}
12441240

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

1252-
process.nextTick(emitListeningNT, self);
1248+
process.nextTick(emitListeningNT, this);
12531249
};
12541250

12551251

@@ -1509,15 +1505,14 @@ Server.prototype.close = function(cb) {
15091505

15101506
Server.prototype._emitCloseIfDrained = function() {
15111507
debug('SERVER _emitCloseIfDrained');
1512-
var self = this;
15131508

1514-
if (self._handle || self._connections) {
1509+
if (this._handle || this._connections) {
15151510
debug('SERVER handle? %j connections? %d',
1516-
!!self._handle, self._connections);
1511+
!!this._handle, this._connections);
15171512
return;
15181513
}
15191514

1520-
process.nextTick(emitCloseNT, self);
1515+
process.nextTick(emitCloseNT, this);
15211516
};
15221517

15231518

lib/repl.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ function regexpEscape(s) {
11081108
REPLServer.prototype.convertToContext = function(cmd) {
11091109
const scopeVar = /^\s*var\s*([_\w\$]+)(.*)$/m;
11101110
const scopeFunc = /^\s*function\s*([_\w\$]+)/;
1111-
var self = this, matches;
1111+
var matches;
11121112

11131113
// Replaces: var foo = "bar"; with: self.context.foo = bar;
11141114
matches = scopeVar.exec(cmd);
@@ -1117,9 +1117,9 @@ REPLServer.prototype.convertToContext = function(cmd) {
11171117
}
11181118

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

11251125
return cmd;

test/parallel/test-zlib.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,10 @@ SlowStream.prototype.resume = function() {
125125

126126
SlowStream.prototype.end = function(chunk) {
127127
// walk over the chunk in blocks.
128-
var self = this;
129-
self.chunk = chunk;
130-
self.length = chunk.length;
131-
self.resume();
132-
return self.ended;
128+
this.chunk = chunk;
129+
this.length = chunk.length;
130+
this.resume();
131+
return this.ended;
133132
};
134133

135134

0 commit comments

Comments
 (0)