28 July 2023

AI's Take On Javascript

I asked Bing AI:

in javascript, how to access outer object’s this from inner objects function ?

The answer starts with:

To access the outer object’s this from an inner object’s function in JavaScript, you can assign the outer object’s this to a local variable and use that variable inside the inner object’s function. Here is an example:

var outerObject = {
  outerProperty: 'outerPropertyValue',
  innerObject: {
    innerFunction: function() {
      console.log(outerObject.outerProperty);
    }
  }
};

outerObject.innerObject.innerFunction(); // logs 'outerPropertyValue'

To name the outer object - not a bad idea at all, only it was not applicable for me.

But then, there was more:

You can also use arrow functions in ES6 to capture the this value of the enclosing context, avoiding the need to assign this to a new variable or to use bound functions. Here is an example:

var outerObject = {
  outerProperty: 'outerPropertyValue',
  innerObject: {
    innerFunction: () => {
      console.log(this.outerProperty);
    }
  }
};

outerObject.innerObject.innerFunction(); // logs 'outerPropertyValue'

That looks nice and simple! But, isn’t innerFunction’s this the innerObject? 🤔

Running the code produces an expected undefined.

So, nothing new - AI offers convincing anwers which are wrong - it’s only that it happened to me for the first time.

08 May 2023

Text Style Tool

I’ve added a tool for styling / formatting text. It works by replacing regular letters of the alphabet with mathematical and other symbols defined in Unicode standard. Downsides are that it does not support non-alphabet letters and is not accessible.

Background

One day I wanted to emphasise part of the text I was posting to a social network, but there was no option for making the text bold or italic. I remembered that I saw people (ab)use Unicode for that and went to search for the web tool. There were some, but with rather poor page layout and filled with ads. So I thought - how hard can it be? After a few minutes, a Stackoverflow answer referred me to the excelent piece of Javascript which does the thing. With that, I had the main building block and after a while my version of the text formatter was done. It has no adds, runs fast and has only styles I needed so far - bold, italic and script. Perhaps underline and strikethru should be added also…

06 December 2022

Catch Throw

Have you ever wondered why ocasionally you can find some code along the lines of:

try
{
    SomeFancyThing();
} 
catch 
{
    throw;
}

On the face of it, it is a noop. It doesn’t wrap the exception into a new, more likable, one. It doesn’t prevent propagating of the exception. It doesn’t even log the exception. So, it does the same (ok, it also slows the execution down a little bit) as just writing

SomeFancyThing();

However, when lines of code are taken into account, things change dramatically. Instead of one line code, there is eight… 🤔 So, if productivity is measured by contributed LoC one can dramatically improve productivity by using this approach.

More weirdness

Worse, ofcourse, are

try
{
    SomeFancyThing();
} 
catch 
{
    
}

and

try
{
    SomeFancyThing();
} 
catch (Exception e)
{
    throw e;
}

In first example, at least log the exception so you know something bad happened. In second, stack trace is destroyed.

And still more

try
{
    SomeFancyThing();
} 
catch (Exception e)
{
    throw new MyFancyException(e.Message);
}

I have a gut feeling this somehow originates from Java. Or it’s forced by some team agreement on wrapping all exceptions. Then at least make it

throw new MyFancyException(e);

when throwing to preserve the original exception. You’ll thank yourself later.