简体   繁体   English

当我的console.log类型的函数返回一个对象时,它的类型为undefined。 我不明白为什么

[英]When I console.log type of my function which returns an object it gives undefined. I don't understand why

I can't understand why the result of this code is undefined? 我不明白为什么这段代码的结果不确定?

function f() {
    return
    {
        x: 0
    };
}
console.log(type of f());

typeof is a single word. typeof是一个单词。 Also, you are invoking your function, thus you are evaluating it's returned value, which is undefined in your code because of automatic semicolon insertion. 另外,您正在调用函数,因此您正在评估它的返回值,由于自动分号插入,该值在代码中未定义。

from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return 来自https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/return

The return statement is affected by automatic semicolon insertion (ASI). return语句受分号自动插入(ASI)的影响。 No line terminator is allowed between the return keyword and the expression. return关键字和表达式之间不允许使用行终止符。

 function f() { return //this is treated as though it had a ; { x: 0 }; } console.log(typeof f()); console.log(typeof f); 

 function f() { return { x: 0 }; } console.log(typeof f()); console.log(typeof f); 

暂无
暂无

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

相关问题 为什么当我执行console.log时,函数中的对象返回为“ undefined”? (了解创建阶段,执行阶段) - Why when I do a console.log is my object within a function returned as 'undefined'? (Learning about creation phase, execution phase) 当 map 函数在代码中时,它返回 undefined,当 console.log 时,它返回一个空对象和一个数据。 我只想要数据 - when map function is in the code it returns undefined and when console.log it returns the an empty object and an the data. i just want the data 为什么我console.log一个对象,它显示对象,但是当我console.log时,它显示未定义 - Why I console.log a Object,it shows object,but when I console Object.value ,it shows undefined 为什么console.log函数返回undefined? - Why console.log function returns undefined? 我的console.log放在错误的位置,但是我不明白为什么 - My console.log is in the wrong place but I do not understand why 为什么 firebase 在我的组件中返回 undefined 但我在读取数据库时可以在 console.log 中看到 object? - Why does firebase return undefined in my component but I can see the object in console.log when I read my database? 为什么我没有在 JS 中得到 console.log - Why i don't get the console.log in JS Uncaught TypeError: pushRoom.on is not a function 我 console.log 它但不知道如何解决它 - Uncaught TypeError: pushRoom.on is not a function which I console.log it but don't know how to solve it 用于返回数组的函数返回undefined。 函数内的Console.log(数组)返回数组 - Function that is intended to return an array is returning undefined. Console.log(array) within function returns the array 当我尝试在函数中添加一些结果时,为什么 console.log 打印到控制台“未定义”? - Why the console.log prints to the console "undefined" when I try to add some results in the function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM