You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FunctionDeclaration返回值疑问:Chrome DevTools与ES规范不符?

Why Does Chrome DevTools Show the Function in the Second Example Instead of undefined?

Let’s start with the two test cases you ran in Chrome DevTools:

Example 1

// First script
function a(){} 
// Outputs: undefined

Example 2

// Second script
{ function a(){} } 
// Outputs: ƒ a(){}

As you pointed out, the ECMAScript specification clearly states that evaluating a FunctionDeclaration must return NormalCompletion(empty) — which corresponds to undefined in the console. Firefox strictly follows this rule, returning undefined for both examples. So why does Chrome behave differently?

This isn’t a bug in Chrome DevTools itself — it’s about how the V8 engine (which powers Chrome) handles block-level function declarations in non-strict mode, using an alternative semantics outlined in the spec’s Appendix B.3.3 (the note you noticed: "An alternative semantics is provided in B.3.3").

Here’s a clear breakdown:

  • The core spec requires function declarations to always evaluate to undefined, no matter where they’re placed.
  • Appendix B is a set of non-normative extensions for web compatibility. In non-strict mode, V8 uses this extension to treat block-internal function declarations like function expressions. When the block is evaluated as an expression (like in your second example), it returns the function object instead of undefined.
  • If you switch to strict mode, Chrome will align with the core spec and match Firefox’s behavior. Try running this:
    'use strict';
    { function a(){} } 
    // Outputs: undefined
    

To answer your question directly: No, this isn’t a Chrome DevTools issue. It’s a deliberate web-compatibility choice by the V8 engine for non-strict mode block-level functions.

内容的提问来源于stack exchange,提问作者MaximPro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:47:38