简体   繁体   English

Javascript-具有new关键字的函数构造函数

[英]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 . 在功能方面,没有barthis You need to remove the this part of the this.bar . 您需要删除this.barthis部分。 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.

相关问题 javascript:关于构造函数的问题**新**关键字 - javascript: question about constructor **new** keyword 如何用构造函数/new关键字写typescript? - How to write typescript with constructor function/new keyword? 函数中的Javascript'new'关键字 - Javascript 'new' keyword within function 为什么在对象构造函数Function Javascript中使用此关键字 - Why use this keyword in object constructor Function Javascript JavaScript新的Function表达式和新的Function构造函数 - JavaScript new Function expression and new Function constructor Javascript:使用新关键字构造函数的词法范围问题 - Javascript: lexical scoping issue using new keyword constructor 如何在JavaScript中创建构造函数,以便如果使用'new'关键字创建的两个实例,instance1 === instance2的值返回true - How to create a constructor function in javascript so that if two instance created with 'new' keyword the value of instance1 === instance2 return true 函数返回数组的Javascript`new`关键字 - Javascript `new` keyword on function returning array 用 new 关键字声明 function 并在 javascript 中自行调用它 - declaring function with new keyword and self invoke it in javascript 如何使用this关键字向构造函数添加新属性? - How to add a new property to constructor function using the this keyword?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM