'this' and 'self'

Hi guys,

have just joined the team and am very happy to be contributing to the HiFi streamer that I now use on two rooms in my flat!

I wanted to open a discussion about something I see throughout the Volumio source code that I think could do with changing (possibly). I thought it might be a fun discussion :slight_smile:

BTW I am not a javascript expert in anyway, but think I know about this bit.

In nearly all the source code, at the top of every method, a reference called ‘self’ is made that points to ‘this’. The ‘self’ reference is then used exclusively to reference the current object. Most of the time this ‘self’ reference is entirely unnecessary and actually makes the code harder to read.

The only time you actually need to do this is when you wish the ‘this’ in an outer scope to be available at runtime in an inner scope that you are declaring - e.g. (in pseudocode)

This does not work

[code]function A() {

console.log(this); // resolves now to A

this.onSomeEvent = function() {
console.log(this); // resolves later to the function not A


This ‘this’ inside the function is lexically scoped at runtime to the function itself.

In normal javascript this is worked around by creating a ‘self’ reference to ‘this’ before defining the function

[code]function A() {

console.log(this); // resolves now to A

var self=this;
this.onSomeEvent = function() {
console.log(self); // resolves later to A


As far as I am aware this is the only time you need to create a reference to ‘this’, otherwise you can use this quite safely.

The problem I have is that creating a ‘self’ reference implies you are going to use it in a callback later, but most of the time that is not happening - it makes the code harder to understand. Only using ‘self’ when it is required will make it clearer what was intended (i.e. we are going to us this in a callback).

The latest version of the alsa controller index.js is coded in this style (I just updated it).

github.com/volumio/Volumio2/blo … r/index.js

You can see only one reference to ‘self’ is needed for the client callback.

What do you all think?

I admit that I followed the coding style of a former contributer, without asking too much questions too myself. And therefore this discussion is heavily useful!
Let me document a bit more on this one and I’ll hit you back…

Self is a way to make it easier to cope with prototyped inheritance and scoping. I can relate it could be useful, but people need to grab the concept. The unfortunate part of javascript that object orientated and all the changes to ECMAScript 2016 can be a bit overwhelming. There is no real standard…that is what making community driven software with JS annoying :wink:

I prefer myself, working in communities or large groups, with the module pattern, rather than prototyping. Prototyping could be useful on big playists or album/song with large amounts of records. That could be something to be considered, and set a few “design” or “code” principles that helps everyone to develop in an agreed matter. Sometimes results in slightly less performing code, but it can help us.

However, I think we might not need to much javascript outside working with Angular in order to work. As you mentioned in the other topic, the performance of Angular can be a bit dodgy. I’ve not worked really with Angular, but I know a bit from under the hood. I am curious how Angular 2 would do (also implementation of the dom in memory, to let it render faster)