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

前端 未结 5 603
天涯浪人
天涯浪人 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:12

    name is a special property because it gives the name of the function when defined like this:

    function abc(){
    
    }
    

    In this case name would return the string "abc". This name cannot be changed. In your case, the function does not have a name, hence the empty string.

    http://jsfiddle.net/8xM7G/1/

提交回复
热议问题