Console returns undefined [duplicate]

so ive hijacked 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

submitmsg() is my custom function

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.


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)
};

Answers:

Answer

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.

For example:

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

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

  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.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.