Home > database >  How can I make the Javascript console NOT print the return value of a function
How can I make the Javascript console NOT print the return value of a function

Time:12-20

I have a function, it looks similar to this (I'm making an obfuscation program for those who are interested, that's why I wont share the actual code)

function compile(r,b){
//Do 'stuff' to the inputs
console.log(enc);
return enc
}

basically I want to be able to nest functions but not double display the output (double display would look like this:

> compile("value","value")
< output
< output

I dont want this, but I cant remove the console.log because the output isnt the exact same. if I remove the return statement it does this:

> compile("value","value")
< output
> decompile(compile("value","value"))
< undefined

it gives undefined because the compile function doesnt return anything.

The reason I "can't" remove the console.log is as follows:


> console.log("Hi\nHow are you?")
< Hi
< How are you?

return:

> function example(){
> return "Hi\nHow are you?"
>}
< 'Hi\nHow are you?'

This is why I want to keep the console.log statement as you can see they do "the same thing" but display their output differently

How do I make it to the return value of a function is invisible?

CodePudding user response:

There are a few ways you could approach this problem, depending on your specific needs. Here are a few options:

1.You can simply remove the console.log statement from your compile function, and return the enc value directly. Then, you can use the compile function as an input to another function, like decompile, without logging the output twice.

2.If you need to keep the console.log statement for some reason, you could modify your decompile function to not log the output of compile. Alternatively, you could wrap the compile function in another function that suppresses the output, like this:

function suppressOutput(func, suppress) {
  return function(...args) {
    const output = func(...args);
    if (!suppress) {
      console.log(output);
    }
    return output;
  }
}

const compileWithoutOutput = suppressOutput(compile);
 console.log(decompile(compileWithoutOutput("value", "value")));

This will allow you to use compile as an input to decompile without logging the output twice.

3.Another option is to use a logging library that allows you to selectively turn on or off logging for different parts of your code. This can be useful if you need to keep the console.log statement in your compile function, but want to suppress the output in certain situations.

I hope this helps! Let me know if you have any questions or if you'd like more information.

CodePudding user response:

I want to keep the console.log statement - as you can see they do "the same thing" but display their output differently"

When you want to keep the console.log to have it print the string on multiple lines, you should keep it out(side) of the function, and log the call result:

console.log(compile("value","value"))

If this is too cumbersome to use, write a second function besides compile:

function compile(r, b) {
  // Do stuff to the inputs
  return enc;
}
function compileAndPrint(r, b) {
  const enc = compile(r, b);
  console.log(enc);
  // return enc;
}

then you only need to call

compileAndPrint("value","value")

If you have multiple functions where you need this, you can also write a generic decorator:

function withPrintResult(fn) {
  return function(...args) {
     const result = fn.apply(this, args);
     console.log(result);
     return result;
  }
}
const compileAndPrint = withPrintResult(compile);
  • Related