javascript 控制台返回未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11360904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:54:34  来源:igfitidea点击:

Console returns undefined

javascript

提问by Fibrewire

so ive hiHymaned the console function

所以我劫持了控制台功能

var log = Function.prototype.bind.call(console.log, console);
console.log = function (a) {
    log.call(console, a);
    submitmsg("Log", a);
};

this has the desired effect however it also returns "undefined" as an unexpected bonus

这具有预期的效果,但它也会返回“未定义”作为意外奖励

I cant figure out why which leads me to think there is something slightly wrong here

我不明白为什么这让我觉得这里有点不对劲

enter image description here

在此处输入图片说明

Hello world is generated by log.call(console, a)as expected

Hello worldlog.call(console, a)按预期生成

submitmsg()is my custom function

submitmsg()是我的自定义功能

this is working exactly how i want, as i said though im slightly concerned that it is also returning "undefined" for reasons i do not understand.

这正是我想要的,正如我所说,虽然我有点担心它也会因为我不明白的原因返回“未定义”。



Note:The following code was posted by the OP as an answer to the question. The comments on the answer have been moved to the comments on the question.

注意:OP 发布了以下代码作为问题的答案。对答案的评论已移至对问题的评论。



So the correct code should be the following?

那么正确的代码应该如下?

var log = Function.prototype.bind.call(console.log, console);
console.log = function (a) {
    return log.call(console, a);
    submitmsg("Log", a)
};

回答by James Allardice

If I've understood your question correctly, it's because you are not explicitly returning anything from the function. When you don't return a value from a function, it implicitly returns undefined.

如果我正确理解了您的问题,那是因为您没有从函数中明确返回任何内容。当您不从函数返回值时,它会隐式返回undefined.

For example:

例如:

function example() {}
console.log(example()); //undefined

This is defined in the [[Call]]internal method specification(relevant points in bold):

这在[[Call]]内部方法规范中定义(相关点以粗体显示):

  1. Let funcCtx be the result of establishing a new execution context for function code using the value of F's [[FormalParameters]] internal property, the passed arguments List args, and the this value as described in 10.4.3.
  2. Let result be the result of evaluating the FunctionBody that is the value of F's [[Code]] internal property. If F does not have a [[Code]] internal property or if its value is an empty FunctionBody, then result is (normal, undefined, empty).
  3. Exit the execution context funcCtx, restoring the previous execution context.
  4. If result.type is throw then throw result.value.
  5. If result.type is return then return result.value.
  6. Otherwise result.type must be normal. Return undefined.
  1. 令 funcCtx 成为使用 F 的 [[FormalParameters]] 内部属性的值、传递的参数 List args 和 this 值为函数代码建立新的执行上下文的结果,如 10.4.3 中所述。
  2. 令 result 是对 FunctionBody 求值的结果,FunctionBody 是 F 的 [[Code]] 内部属性的值。如果 F 没有 [[Code]] 内部属性或其值为空的 FunctionBody,则结果为(正常、未定义、空)。
  3. 退出执行上下文funcCtx,恢复之前的执行上下文。
  4. 如果 result.type 是 throw,则抛出 result.value。
  5. 如果返回 result.type,则返回 result.value。
  6. 否则 result.type 必须是正常的。返回未定义。