Thursday, April 26, 2018

Function Object - name property


Function objects contain some properties.

1. name property

We can access a function’s name with the help of the “name” property.


function testFunctionName() {
  alert("Hi");
}

alert(testFunctionName.name); //testFunctionName


let sayHi = function() {
  alert("Hi");
}

alert(sayHi.name); // sayHi

Above code, you can see we are getting the function name with the help of name property.


It will work with function which is placed inside object too. See below to understand further,


let user = {

  sayHi() {
    // ...
  },

  sayWelcome: function() {
    // ...
  }

}

alert(user.sayHi.name); // sayHi
alert(user.sayWelcome.name); // sayWelcome


Note: Suppose if there is no name for function then empty string will be returned.






No comments:

Post a Comment