[英]Javascript - function constructor with new keyword
When the below foo
function is called with new
, it turns into a constructor function returning the object which gets assigned to baz
. 当下面的
foo
函数用new
调用时,它将变成一个构造函数,该函数返回分配给baz
的对象。
function foo() {
this.baz = "baz";
console.log(`${this.bar} ${baz}`);
}
var bar = "bar";
var baz = new foo(); // prints undefined undefined
The question is why baz
inside the console statement gets undefined
value - would it be right to say that while baz
got hoisted, when new foo() is called, till the function returns the baz
is not assigned the returned object ? 问题是,为什么控制台语句内的
baz
会获得undefined
值-可以说在提升baz
同时调用new foo()直到函数返回baz
未分配返回的对象,这是正确的吗?
console.log(baz)
in a later line prints the returned object correctly. 后面一行中的
console.log(baz)
正确打印返回的对象。
In the function there is no bar
in the this
. 在功能方面,没有
bar
在this
。 You need to remove the this
part of the this.bar
. 您需要删除
this.bar
的this
部分。 For baz
you must use the this.baz
, because it is attached to the object instance. 对于
baz
,必须使用this.baz
,因为它已附加到对象实例。
function foo() { this.baz = "baz"; console.log(`${bar} ${this.baz}`); } var bar = "bar"; var baz = new foo();
This code means 该代码意味着
this.baz = "baz";
console.log(`${this.bar} ${baz}`);
that when you create an object it will have only baz
in it. 当您创建一个对象时,它将只有
baz
。 In the body if you want to access the object's property you need to access via this
. 在主体中,如果要访问对象的属性,则需要通过
this
进行访问。 ${baz}
is undefined because there is no baz
variable(it will not look in the this
).So if you need to access the baz
in the function you need to use this.baz
. ${baz}
是未定义的,因为没有baz
变量(它不会在this
查找)。因此,如果您需要在函数中访问baz
,则需要使用this.baz
。
what about ${this.bar}
, there is no bar
property in the this
, only baz
. ${this.bar}
, this
没有bar
属性,只有baz
。
The variable baz
doesn't exist in the moment of executing the constructor. 变量
baz
在执行构造函数时不存在。 baz
is created after the constructor is executed. 在构造函数执行后创建
baz
。
It would work similarly if you used an IIFE: 如果您使用IIFE,它的工作原理类似:
var test = (function() {
console.log(test) // undefined
return 'test'
}())
console.log(test) // 'test'
MDN link will help you to understand what this
means. MDN链接将帮助您了解
this
含义。
Used inside a function, this refers to the current object.
在函数内部使用,它指向当前对象。 What that actually means is specified by the way in which you called that function.
实际含义由调用该函数的方式指定。 If you called it using dot notation or bracket notation on an object, that object becomes this.
如果您在对象上使用圆点符号或方括号符号来调用它,则该对象将变为此。 If dot notation wasn't used for the call, this refers to the global object.
如果呼叫未使用点符号,则表示全局对象。
So you are calling this.bar
inside of function foo()
. 因此,您正在
function foo()
内调用this.bar
。 You will get undefined
because function foo() doesn't have bar
property. 您将得到
undefined
因为函数foo()没有bar
属性。
Also, when you call just bar
inside of function foo ()
, It will starts to find bar property. 另外,当您仅在
function foo ()
内调用bar
时,它将开始查找bar属性。
First inside of function foo ()
and search it if global
has bar property. 首先在
function foo ()
内部,然后搜索global
是否具有bar属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.