2

All objects are a collection of Key-Value pairs in JavaScript. such as:

let foo = {
   name : "person"
}

Then, name is the Key and "person" is the value.

However, functions are also objects, so what would the key value pairs be in the following situation:

let foo = {
   console.log("Hello")
}
  • 6
    None of the JS in your question is valid. – Andy Feb 07 '19 at 21:20
  • 4
    Neither of those are valid javascript. The first should be `let foo = { name: "person" };`, and the second one simply wouldn't do anything. – Duncan Thacker Feb 07 '19 at 21:20
  • 1
    Try `console.log({ func: function(x) { return x + x; } })` in a modern browser’s devtools if you’re curious about the properties of a first class function in JS – MTCoster Feb 07 '19 at 21:21
  • 2
    ignoring the fact that the code you show isn't JS: this is what https://www.ecma-international.org/publications/standards/Ecma-262.htm exists for, which is the only true explanation of the things you're asking about. Is it a bit dense at times? Sure. Is it also the only correct answer that doesn't dumb things down to a degree where you'll just end up asking questions based on misunderstanding the dumbed down explanation instead of the real answer? Absolutely. Is it an amazing read if you want to understand something about the nature of JS? Very much also yes. – Mike 'Pomax' Kamermans Feb 07 '19 at 21:21
  • [How is everything in JS an object](https://stackoverflow.com/questions/9108925/how-is-almost-everything-in-javascript-an-object) - basically a duplicate question. – Andy Feb 07 '19 at 21:23
  • This might be easier to digest for an intro to js functions https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function – Austin Greco Feb 07 '19 at 21:25
  • There is also a syntax error in the second example but I think that you ment it so I left it there. – Stav Alfi Feb 07 '19 at 21:30
  • @StavAlfi even with your edit it is still an error.... going to be something like "Uncaught SyntaxError: Invalid shorthand property initializer" – epascarello Feb 07 '19 at 21:41
  • Not everything an object contains are properties (key-value pairs), and not all objects can be represented as object literals. – Bergi Feb 07 '19 at 21:41

2 Answers2

2

While its true that functions are objects, the process of declaring one does not give it any custom keys. There are however some keys that it gets automatically, and you can also add extras after the fact if you want

Here's an example of creating a function, and then accessing some of the properties it gets automatically:

 function sample(a) {
  console.log('hello', a);
 }
 
 console.log(sample.name); // 'sample', since that's what i called it
 console.log(sample.length); // 1, because i specified one argument (a)
 console.log(sample.toString); // all functions inherit a number of methods, and toString is one of them
 console.log(sample.toString()); // now i'm calling to string
 

If you'd like to see more examples of which keys get created automatically for functions, i'd recommend looking at this page. Anything that starts with Function.prototype or Object.prototype is inherited by all functions.

As i mentioned, you can add any keys you want onto a function object once you've created it. For example:

function sample(a) {
  console.log('hello', a);
}

sample.metadata = "This function was created by nick";
sample.golfHandicap = 42;

console.log(sample.metadata);
console.log(sample.golfHandicap);
Nicholas Tower
  • 72,740
  • 7
  • 86
  • 98
  • Very good answer; I might suggest clarifying that "properties" are the same thing they're calling "keys" but other than that this is well explained! – Daniel Beck Feb 07 '19 at 21:57
0

a function can be defined and nested in various ways:

// defined globally. attached to window object
function runnable(){
}

// above example is same as:
window.runnable=function(){
}

// function can be attached to an object in various ways
// 1:
var obj={
   callMe:function(){
   },
   fetchMe:runnable
}
// 2:
obj.anotherFunc=function(){
}
obj.yetAnother=runnable;
obj.anotherOne=window.runnable;

// functions can also be defined in es6 lambda style
behzad besharati
  • 5,873
  • 3
  • 18
  • 22