Why can't I set a JavaScript function's name property?

前端 未结 5 607
天涯浪人
天涯浪人 2020-12-04 00:37

I am learning JavaScript and read that functions are like objects and can have properties set like this:

var person = function(){
}
person.name=\"John Smith\         


        
5条回答
  •  余生分开走
    2020-12-04 01:24

    The name property is set by the Function constructor and cannot be overwritten directly. If the function is declared anonymous, it will be set to an empty string.

    For example:

    var test = function test() {};
    alert(test.name); // Displays "test"
    test.name = "test2";
    alert(test.name); // Still displays "test"
    

提交回复
热议问题