这是简单的练习代码
const greet = (person) => {
return `Hey ${person}!`;
}
我调用greet的时候一直报上面的错误。这也适用于我编写的几乎所有代码,当我调用它们时,我得到相同的引用错误并且找不到变量。我做错了什么?
我试过了
const greet = (person) => {
return `Hey ${person}!`;
}
greet()
然后调用它。它仍然带来同样的错误。我知道这很简单,但我就是不能把手指放在上面
你的函数声明没有问题。这就是我们在 Js 中使用箭头函数声明函数的方式。
// Declaration of Function
const greet = (person) =>{
console.log(`Hey There ${person}`);
}
只要你为这个人提供价值,这就应该完美地工作。
greet(); // This will give, Hey there undefined
greet("joseph"); // This will provide, Hey there joseph
如果您收到异常说明
greet()
函数不存在,请确保您在使用它的函数之前声明它。因为箭头函数应该在你使用它的地方之前声明,所以它们与 JavaScript 中的普通函数相比有不同的行为。
// If using arrow syntax, *must* declare functions beforehand
const greet = (person)=> {
console.log(`Hey There ${person}`);
}
const useGreet = () =>{
greet("joseph");
}
function main(){
useGreet();
doSomething();
}
// If using function syntax can declare functions anywhere
function doSomething(){
console.log("Do Something");
}