[英]How can I call a function from another function inside a Class
I have the following problem: I want to call the function printHello
from the function testHello
.我有以下问题:我想调用的函数
printHello
从功能testHello
。 The printHello
function works on its own, however, when I try to call printHello
from the testHello
function, I get a reference error. printHello
函数独立工作,但是,当我尝试从testHello
函数调用printHello
,出现引用错误。 Thank you for your help.感谢您的帮助。
class Test { constructor(name) { this.name; } printHello(parameter) { console.log(parameter); } testHello() { printHello(printHello(this.name)); } } var test = new Test("Sandro"); test.printHello("hello"); //works, prints "Hello" to the Console test.testHello(); // does not work: Reference Error: printHello is not defined
Use this
keyword.使用
this
关键字。 Also, you had a few bugs (i commented them)另外,你有一些错误(我评论了它们)
class Test{ constructor(name){ this.name = name; // <- you need to assign the `name` to `this.name` } printHello(parameter){ console.log(parameter); } testHello(){ this.printHello(this.name); // <- you had double invocation here } } var test = new Test("Sandro"); test.printHello("hello"); //works, prints "Hello" to the Console test.testHello(); // does not work: Reference Error: printHello is not defined
You probably need to call你可能需要打电话
this.printHello(this.name);
inside your testHello Function.在您的 testHello 函数中。
A couple of issue in the code that can be easily fixed.代码中的几个问题可以轻松修复。
1) You need to set the this.name
property in the constructor. 1) 您需要在构造函数中设置
this.name
属性。
2) You want to prefix the call to printHello
within testHello
with this.
2) 您想在
testHello
为printHello
的调用添加前缀this.
class Test { constructor(name) { //You need to actually set this.name to the parameter value this.name = name; } printHello(parameter) { console.log(parameter); } testHello() { //this keyword is needed to prefix the method name this.printHello(this.name); } } //Now below should work with no issues var test = new Test("Sandro"); test.printHello("hello"); test.testHello();
class Test { constructor(name) { this.name = name; } printHello(parameter) { console.log(parameter); } testHello() { this.printHello(this.name); } } var test = new Test("Sandro"); test.printHello("hello"); //works, prints "Hello" to the Console test.testHello(); // does not work: Reference Error: printHello is not defined
var test is defined on the global level. var test 是在全局级别定义的。 This returns an object that contains name property.
这将返回一个包含 name 属性的对象。 In the prototypes, we can see constructor, printHello and testHello functions.
在原型中,我们可以看到构造函数、printHello 和 testHello 函数。 Now when you call //test.printHello// it will work without a problem.
现在,当您调用 //test.printHello// 时,它将毫无问题地工作。 Why?
为什么? when the function is not at the same level, then it will go down to prototypes until it meets the function.
当函数不在同一层级时,就会下到原型,直到遇到函数。 In the prototype, you can see the printHello function.
在原型中,您可以看到printHello 函数。 This is call prototype inheritance.
这就是调用原型继承。
Now what happens in //test.testHello()// .现在在 //test.testHello()// 中发生了什么。 javaScript trying to execute this one and see another function inside that.
javaScript 试图执行这个并查看其中的另一个函数。 So it will try to find where this printHello function defined!.
所以它会尝试找到这个 printHello 函数定义的位置!。
Now we need to know about the lexical environment.现在我们需要了解词法环境。 Check this sample,
检查这个样本,
var test = 'hello' ;
function foo(){
var test = 'world'
console.log(test)
}
Now what will happen if you call foo()?现在如果你调用 foo() 会发生什么? it will console.log 'world',
它将 console.log 'world',
Now I'm removing test var inside the function现在我正在删除函数内的 test var
var test = 'hello' ;
function foo(){
console.log(test)
}
Now let's call foo();现在让我们调用 foo(); The output will be 'hello'
输出将是“你好”
The same thing happens with your code.你的代码也会发生同样的事情。 Once javascript trying to execute test.testHello(), it sees printHello() inside that function.
一旦 javascript 尝试执行 test.testHello(),它就会在该函数中看到 printHello()。 Now it will try to find the function at the same level.
现在它将尝试找到同一级别的函数。 It is not defined in the testHello() function neither inside prototypes of testHello().
它没有在 testHello() 函数中定义,也没有在 testHello() 的原型中定义。 So now javaScript trying to find out where is this testHello() function defined.
所以现在 javaScript 试图找出这个 testHello() 函数在哪里定义。 It is on the global level because var test is defined on the global level.
它在全局级别,因为 var test 是在全局级别定义的。 Is this printHello() defined at the global level?
这个 printHello() 是在全局级别定义的吗? NO!
不! It is inside the prototype of that object.
它位于该对象的原型中。 Now javaScript cannot find the printHello function in global level either.
现在javaScript 也找不到全局级别的printHello 函数。 So it will throw an error that saying printHello() is undefined.
所以它会抛出一个错误,说 printHello() 是未定义的。 'this' keyword help you to refers to the object it belongs to.
'this' 关键字帮助您引用它所属的对象。 Simply, with 'this' keyword we are asking please check the test object's prototype before search on the global level.
简单地说,使用“this”关键字,我们要求在全局级别搜索之前检查测试对象的原型。 (notice that not in the testHello() prototypes but in the test object prototype)
(注意不是在 testHello() 原型中,而是在测试对象原型中)
Something important to understand is functions are not actually functions.需要理解的重要一点是函数实际上并不是函数。 They are objects in javaScript!!!
它们是 JavaScript 中的对象!!! Use 'this' keyword.
使用“this”关键字。 It will help.
我会帮你的。 Also, try to learn about prototype inheritance and lexical environment.
此外,尝试了解原型继承和词法环境。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.