Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many ways are there to see all properties that you manually add to the function? JavaScript. JS

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?

like image 670
Yellowfun Avatar asked Mar 24 '18 09:03

Yellowfun


People also ask

How do you list all properties of an object in JS?

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.

How many ways can you define a function in JavaScript?

There are 3 ways of writing a function in JavaScript: Function Declaration. Function Expression. Arrow Function.

How many ways you can access the object properties?

An object property can be accessed in two ways. One is . property and the other is [property].

How can you find the number of properties in an object JavaScript?

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.


1 Answers

  Console.log( Object.keys(a) );
like image 118
John Willson Avatar answered Sep 20 '22 03:09

John Willson