In JavaScript functions are first-class objects, that means you can treat them just like any object, in this case, you are only adding a property to the function object.
Ok, let's add some properties to a function.
function a() {
a.firstProp = "I'm 1st - property";
}
a.seccondProp = "I'm 2st - property";
function b() {
a();
}
b(); // initialize 1st property (firstProp) for `function a`;
console.log( Object.getOwnPropertyNames(b) );
console.log( Object.getOwnPropertyNames(a) );
How can I see in Chrome DevTools a key-value pair that I added to the function in code example?
getOwnPropertyNames() The Object. getOwnPropertyNames() method returns an array of all properties (including non-enumerable properties except for those which use Symbol) found directly in a given object.
There are 3 ways of writing a function in JavaScript: Function Declaration. Function Expression. Arrow Function.
An object property can be accessed in two ways. One is . property and the other is [property].
Use the Object. keys() method, passing the object you want to inspect, to get an array of all the (own) enumerable properties of the object.
Console.log( Object.keys(a) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With