javascript - 为什么输出给出 'undefined' 而不是 JS 示例中的 'number'?

标签 javascript html this

以下内容到底是如何输出“未定义”而不是“数字”?

<script>

var foo = {
  bar: function(){ return this.baz; },
  baz: 1
}

console.log(typeof (f = foo.bar)());

</script>

Note: The code is exactly right and not a typo. I mean (f = foo.bar)() only here which is a valid code.

最佳答案

因为在执行时,上下文是“window”,所以window对象上没有声明变量“baz”。

关于javascript - 为什么输出给出 'undefined' 而不是 JS 示例中的 'number'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54880743/

相关文章:

html - Div在旋转的div中居中对齐

javascript 对象原型(prototype) this 引用

.replace 回调中的 JavaScript "this"上下文

javascript - JavaScript 中的 HtmlSpecialChars 等价物是什么?

javascript - 无法使用 play() 函数在 React Typescript 中播放音频 |错误 : Property 'play' does not exist

html - CSS 使整个相对主体水平居中,无需包装器或滚动条?

html - 为什么我的列不会 float 到容器的左边缘?

javascript - 为什么我的正则表达式长度为 2?

javascript - ng build 上 app.module 中的单例服务错误

C# 代码样式 : Switching from "this." prefix to "underscore"