How Javascript Assigns `name` Attribute Of Function?
Solution 1:
Javascript declares the name
variable of the function by taking the left-hand side argument that is equal to the function, which is 'aaa'
in all basic cases. In the first complex definition, you declared state above Javascript will take the variable ab
and assign it to a function making the name 'ab'
. In the final example, you provided it sets the function equal to a pointer in memory, which is not a defined variable, this sets the name
property to an empty string because arr[0]
is not a variable name but a pointer to memory.
Here is a JSFiddle displaying this.
Solution 2:
Something like this:
Inferred function names
Variables and methods can infer the name of an anonymous function from its syntactic position (new in ECMAScript 2015).
var f = function() {}; var object = { someMethod: function() {} }; console.log(f.name); // "f" console.log(object.someMethod.name); // "someMethod"
Read the entire blog. It will clear all your queries.
Post a Comment for "How Javascript Assigns `name` Attribute Of Function?"