Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing function object's properties from inside the function body

Functions in javascript is also an object and can have properties. So is there any way to access its properties from inside its own function body?

like this

var f = function() { 
  console.log(/*some way to access f.a*/);
};
f.a = 'Test';
f(); //should log 'Test' to console
like image 730
KwiZ Avatar asked Jan 11 '15 12:01

KwiZ


People also ask

How do you access the properties of a function?

Accessing a function as a method To access a property as a method, just define a function to a property and include other properties in that function. In the following example an object called "employee" is created with properties "fullName", "lastName" , "firstName" and "id".

How do you access an object from a function?

To access the object, a method can use the this keyword. The value of this is the object “before dot”, the one used to call the method. Here during the execution of user. sayHi() , the value of this will be user .

What is the correct way of accessing object properties?

You can access the properties of an object in JavaScript in 3 ways: Dot property accessor: object. property. Square brackets property access: object['property']

What is a function inside an object called?

In every method exercise so far, methods are explained as “like functions” or “similar to functions”, since they even use the same keyword and syntax. The only difference I have gathered is that they are used inside an object, while functions are not.


1 Answers

arguments.callee is the function itself and doesn't get affected by the name of the function.

var f = function() { 
  console.log(arguments.callee.a);
};
f.a = 'Test';
f();
like image 187
pranavjindal999 Avatar answered Oct 13 '22 21:10

pranavjindal999