How do javascript functions have properties in them?

后端 未结 4 1139
萌比男神i
萌比男神i 2021-01-21 09:08
function myFunc(){
    console.log(myFunc.message);
}
myFunc.message = \"Hi John\";

myFunc();

Executing the above results in -

Answer:         


        
4条回答
  •  滥情空心
    2021-01-21 09:37

    According to MDN, definition of function is:

    In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are Function objects.

    So here by doing myFunc.message you are adding an additional property to myFunc function( or object) and then it is accessible inside function like other objects.

提交回复
热议问题