Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript-这些构造函数之间有什么区别?_Javascript_Constructor - Fatal编程技术网

Javascript-这些构造函数之间有什么区别?

Javascript-这些构造函数之间有什么区别?,javascript,constructor,Javascript,Constructor,此构造函数与以下构造函数之间的区别是什么: var Person = function(living, age, gender) { this.living = living; this.age = age; this.gender = gender; this.getGender = function() {return this.gender}; } 还有这个: var Person = functio

此构造函数与以下构造函数之间的区别是什么:

    var Person = function(living, age, gender) {
        this.living = living;
        this.age = age;
        this.gender = gender;
        this.getGender = function() {return this.gender};
    }
还有这个:

    var Person = function Person(living, age, gender) {
        this.living = living;
        this.age = age;
        this.gender = gender;
        this.getGender = function() {return this.gender;};
    };

除了被“命名”的构造函数之外,什么都没有。对于#1,
Person.name
将计算为空字符串,对于#2,
Person.name
将计算为
“Person”

将在
函数Person(…)
中设置
name
属性

你可以通过尝试以下方法来了解这一点

var bar = function eigor(){}

然后看看什么是
bar.name

一个是匿名函数,另一个是命名函数。根据您调试的方式和使用的工具,命名函数可能(稍微)更容易调试。谢谢Kevin。谢谢你的帮助!