How do javascript functions have properties in them? - Stack Overflow

function myFunc(){console.log(myFunc.message);}myFunc.message = "Hi John";myFunc();Executin

function myFunc(){
    console.log(myFunc.message);
}
myFunc.message = "Hi John";

myFunc();

Executing the above results in -

Answer: 'Hi John'

How is the function myFunc have the property message on it? typeof myFunc results in "function" and console.log(myFunc) displays the function content (without the property message on it).

How does the above work? Is a function in JavaScript internally an object?

Note - I am aware that functions have other parameters like prototype and length on them. But I am not sure how these are implemented as well.

Additional query - Since console.log(myFunc) does not show the object properties, how do I list all the properties of a function object?

function myFunc(){
    console.log(myFunc.message);
}
myFunc.message = "Hi John";

myFunc();

Executing the above results in -

Answer: 'Hi John'

How is the function myFunc have the property message on it? typeof myFunc results in "function" and console.log(myFunc) displays the function content (without the property message on it).

How does the above work? Is a function in JavaScript internally an object?

Note - I am aware that functions have other parameters like prototype and length on them. But I am not sure how these are implemented as well.

Additional query - Since console.log(myFunc) does not show the object properties, how do I list all the properties of a function object?

Share Improve this question edited Aug 20, 2018 at 8:46 Nithin Kumar Biliya asked Aug 10, 2018 at 10:17 Nithin Kumar BiliyaNithin Kumar Biliya 3,0314 gold badges36 silver badges57 bronze badges 9
  • Yes, JS functions are objects, and as such can have arbitrary properties (not parameters) attached to them. – Amadan Commented Aug 10, 2018 at 10:18
  • Your tag (javascript-objects) already answers it. – Gerardo Furtado Commented Aug 10, 2018 at 10:19
  • The wording in the title of this question does not read correctly. Can you rephrase it correctly in English so it makes sense?! ;-) – JGFMK Commented Aug 10, 2018 at 10:27
  • 1 I think you are confusing the fact that for the syntax, a.b , b is a property - not a parameter of a. Then using incorrect wording still to describe your problem. Nearly everything in Javascript can be represented in object notation. – JGFMK Commented Aug 10, 2018 at 10:32
  • 1 @ChrisM The only edit I made to your correction is just that I removed the correction "parameters/properties", because even if it's the correct terminology I would avoid changing that as it may be part of OP's misunderstanding. Once I did that I just added some formatting, but most of your edits carried on in the current version. – Federico klez Culloca Commented Aug 10, 2018 at 10:55
 |  Show 4 more ments

4 Answers 4

Reset to default 5

How does the above work? Is function in javascript internally an object?

Yes

function example() {};

console.log(example instanceof Object);

Yep, they are objects.

See in particular:

Every JavaScript function is actually a Function object. This can be seen with the code (function(){}).constructor === Function which returns true.

In JavaScript functions are first class objects. This is evidenced by the fact you can assign a function to a variable, pass it as an argument, add properties to it, add it as a property or member of an array etc etc. Sky is the limit.

var myFunction = function(passedFunction){
        passedFunction();
        console.log(myFunction.message);
   };
	 
let functionsArray = [myFunction, function() {
       myFunction.message = "Hello World";
}];
	 
functionsArray[0](functionsArray[1]);

The above outputs "Hello World"

Part of the reason this can seem weird may be in the way functions can be declared. In order to behave a bit like C (but not really) or other languages where a function is in some manner defined prior to the execution of the code proper, the naked 'function' statement 'hoists' the function declaration. So where you use this syntax:

myFunction();
function myFunction(){ 
    console.log("Hello world");
}

What is actually happening is that your function declaration is being 'hoisted' to act as if it was this:

let myFunction = function(){ 
    console.log("Hello world");
}

myFunction();

These two code snippets above are fundamentally equivalent in JavaScript. (N.b. var declarations are also hoisted but let and const are not)

MDN has more about the function declaration and function hoisting.

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744889010a4599291.html

相关推荐

  • How do javascript functions have properties in them? - Stack Overflow

    function myFunc(){console.log(myFunc.message);}myFunc.message = "Hi John";myFunc();Executin

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信