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
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".
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 .
You can access the properties of an object in JavaScript in 3 ways: Dot property accessor: object. property. Square brackets property access: object['property']
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.
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();
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