logo

NJP

Name Property in Functions - Let's Learn ECMAScript 2021 with Earl Duque

Import · Jan 25, 2024 · video

So here's a fun, easy one for you all. If you have a function. So I have
this function called my function. Did you know if you have a function, you can actually say that function name and it will actually tell you
the name of that function as a string? and likewise, if you have a function
that is being assigned to a variable,
an anonymous function, same thing. If you say .name, you'll actually find
the name of that function. So you'll see, here's my function. Here's anonymous function. Both of them are returning the .name. Why am I telling you this? Well, fun fact. The first one that worked in
old JavaScript, but the second one didn't. So this if you're using anonymous
functions, functions that you're using inside of a declaration,
then that .name property didn't exist. But now it does. So fun little fact. And what's the practical
use of this inside ServiceNow? Let's say in your script
you have an array filled with functions. Now, you can actually iterate through them
and know what function is being run whenever you start that function. it's a little bit of a nice way
to keep your code clean and or do good logging statements
for when you call those certain functions.

View original source

https://www.youtube.com/watch?v=LZg_sl2sWSI