Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.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_Closures_This_Scopes - Fatal编程技术网

Javascript 这不';我没有显示正确的名字

Javascript 这不';我没有显示正确的名字,javascript,closures,this,scopes,Javascript,Closures,This,Scopes,为什么console.log显示名称“Alex”?为什么不与“大卫”的名字连在一起 使用方法如下: var Obj = function() { this.name = "Alex"; var that = {}; that.name = "David"; this.objName = this.name + " && " + that.name; //return name; } var o = new Obj(); console(o

为什么console.log显示名称“Alex”?为什么不与“大卫”的名字连在一起

使用方法如下:

var Obj = function() {
    this.name = "Alex";
    var that = {};
    that.name = "David";
    this.objName = this.name + " && " + that.name;
    //return name;
}

var o = new Obj();
console(o.objName);
或者,如果要覆盖
name

var Obj = function() {
    this.name = "Alex";
    var that = {};
    that.name = "David";
    this.name = this.name + " && " + that.name;
    //return name;
}

var o = new Obj();
console(o.name);
使用方法如下:

var Obj = function() {
    this.name = "Alex";
    var that = {};
    that.name = "David";
    this.objName = this.name + " && " + that.name;
    //return name;
}

var o = new Obj();
console(o.objName);
或者,如果要覆盖
name

var Obj = function() {
    this.name = "Alex";
    var that = {};
    that.name = "David";
    this.name = this.name + " && " + that.name;
    //return name;
}

var o = new Obj();
console(o.name);

调用o.name时,它将返回对象的this.name,而不是声明的变量(“name”)。您需要再次更新此.name值

试试这个:

var Obj = function() {
    this.name = "Alex";
    var that = {};
    that.name = "David";
    this.name = this.name + " && " + that.name;
}

var o = new Obj();
console.log(o.name);

调用o.name时,它将返回对象的this.name,而不是声明的变量(“name”)。您需要再次更新此.name值

试试这个:

var Obj = function() {
    this.name = "Alex";
    var that = {};
    that.name = "David";
    this.name = this.name + " && " + that.name;
}

var o = new Obj();
console.log(o.name);

新关键字有问题

一种解决方案是去掉var关键字,然后用this.name替换每个名称

或者更简单地去掉新的关键字。因为它会强制你的对象返回这个

如果您使用的是新关键字,则会发生以下情况:

var Foo=fuction(){
var obj=Object.create(Foo.prototype);
//你的Foo代码
返回obj;

}
新关键字有问题

一种解决方案是去掉var关键字,然后用this.name替换每个名称

或者更简单地去掉新的关键字。因为它会强制你的对象返回这个

如果您使用的是新关键字,则会发生以下情况:

var Foo=fuction(){
var obj=Object.create(Foo.prototype);
//你的Foo代码
返回obj;
}
var Obj=function(){
this.name=“Alex”;
var,它={};
that.name=“大卫”;
this.name=this.name+“&&&”+that.name;
返回名称;
}
var o=新的Obj();
console.log(o.name)

var Obj=function(){
this.name=“Alex”;
var,它={};
that.name=“大卫”;
this.name=this.name+“&&&”+that.name;
返回名称;
}
var o=新的Obj();

console.log(o.name)

当您使用
new
操作符作为构造函数调用
Obj
时,始终会返回一个对象。如果定义的返回值不是
Object
类型,则忽略它。在
Obj
中,要生成的对象由
this
引用,也就是说,分配给
this
的每个属性都将是每个生成对象的属性


当您作为普通函数调用
Obj
时,它只返回定义的返回值,而
将被忽略

name
that
只是局部变量,作用域为
Obj
。它们没有分配给
,因此不是返回对象的一部分。由于返回值
name
的类型为
String
,因此将忽略它(如果作为构造函数调用),而是返回一个对象,其中包含分配给
的所有属性(因此
.name=“Alex”
):


当您使用
new
操作符作为构造函数调用
Obj
时,始终会返回一个对象。如果定义的返回值不是
Object
类型,则忽略它。在
Obj
中,要生成的对象由
this
引用,也就是说,分配给
this
的每个属性都将是每个生成对象的属性


当您作为普通函数调用
Obj
时,它只返回定义的返回值,而
将被忽略

name
that
只是局部变量,作用域为
Obj
。它们没有分配给
,因此不是返回对象的一部分。由于返回值
name
的类型为
String
,因此将忽略它(如果作为构造函数调用),而是返回一个对象,其中包含分配给
的所有属性(因此
.name=“Alex”
):


“名称与局部变量冲突”您在哪里看到冲突?没有冲突。。但是当你呼唤o.name。。它将返回该对象的名称,而不是变量名。“名称与局部变量冲突”您在哪里看到冲突?没有冲突。。但是当你呼唤o.name。。它将返回对象的名称,而不是变量名。不完全正确。“返回名称”完全没用。你说得对。。我忘了把它取下来。。谢谢:)不太对。“返回名称”完全没用。你说得对。。我忘了把它取下来。。谢谢:)“或者更简单地说,去掉新的关键字”这是行不通的。它还需要修改代码的其余部分。“或者更简单地说,去掉新的关键字”这是行不通的。它还需要更改代码的其余部分。