简体   繁体   English

Javascript箭头符号函数未定义的语法错误

[英]Javascript arrow notation functions undefined syntax error

I am new to javascript, i trying to learn to js, here i am getting syntax error while console the output 我是javascript新手,尝试学习js,在控制台输出时出现语法错误

 const screemwarriors = () => { var warriors = "Ninja"; shootwarriors = function(){ console.log(warriors); } } const output = screemwarriors(); console.log(output.shootwarriors()); 

Here i am getting syntax error 在这里我得到语法错误

Looks like your trying to access the result as an object. 看起来您试图作为对象访问结果。 You must return the function in an object if you intend to do it in this way. 如果您打算以这种方式执行此操作,则必须在对象中返回该功能。

Update: cleaned up code 更新:清理代码

 const screemwarriors = () => ({ shootwarriors: () => { console.log("Ninja"); } }); const output = screemwarriors(); output.shootwarriors(); 

Cheers, 干杯,

Your calling code will work if you modify your function like this: 如果您这样修改函数,则调用代码将起作用:

 const screemwarriors = function() { var warriors = 'Ninja'; return { shootwarriors: function() { return warriors; } } } const output = screemwarriors(); console.log(output.shootwarriors()); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM