node.js: Is console.log() a function? [duplicate] - javascript

This question already has answers here:
Why is the method executed immediately when I use setTimeout?
(8 answers)
Closed 8 years ago.
Exactly, is console.log() a function? Why do these following two code snippets have different output?
function delay(x) {
console.log('Start of timeout');
return x;
};
setTimeout(function(){console.log('End of timeout');}, delay(5000));
console.log('Start to do something else');
.
function delay(x) {
console.log('Start of timeout');
return x;
};
setTimeout(console.log('End of timeout'), delay(5000)); // ???????
console.log('Start to do something else');

Yes, console.log is a function.
The first snippet uses the expected syntac for setTimeout. The second calls console.log inline, which returns undefined. That comes the first argument to setTimeout. That explains the different timing of when End of timeout appears.

Related

Function as a callback reloads without respecting the event listener while using arrow-function gives intended behaviour. Why is it so? [duplicate]

This question already has answers here:
What is the difference between a function call and function reference?
(6 answers)
addEventListener calls the function without me even asking it to
(5 answers)
Closed 1 year ago.
Goal
Console must log a message only on clicking the button.
Set-up
const button_l = document.querySelector('.btn-left');
method-1 (Using arrow function as a callback)
button_l.addEventListener('click', () => {
console.log('I am in')
});
method-2 (Using function as a callback)
button_l.addEventListener('click', changeSlide());
function changeSlide() {
console.log('Am I globally present?');
}
Problem Facing
Using method-1 gives the desired results.
Using method-2, the function is called on page load, but when I remove the parentheses (from changeSlide() to changeSlide), it functions as it should, why is it so?
I think it is something very fundamentally obvious that I do not know.
I appreciate a link/reference to any helpful theory/understanding.

JavaScript Function Syntax + setTimeout() 'ERR_INVALID_CALLBACK: Callback must be a function. Received undefined [duplicate]

This question already has answers here:
When do I use parentheses and when do I not?
(5 answers)
Closed 1 year ago.
const sayHello = function () {
console.log("hello");
};
sayHello;
sayHello();
setTimeout(sayHello, 1000);
setTimeout(sayHello(), 1000);
I'm a little confused as to why line 5 'does nothing' but also doesn't throw an error, whereas line 8 runs as expected but line 9 throws the invalid callback error. I'm running this code is Visual Studio Code if it makes a difference.
When you use () after a function name, you're executing the function. But without it, you're referencing the function instead. That is why line 5 doesn't do anything. The function is not being executed. But line 6 on the other hand is executing the function, which should print "hello" to the console.
The setTimeout needs a function reference (the callback function), to let it know what to execute after the 1000 ms has passed.
So typing setTimeout(sayHello(), 1000) executes the sayHello function directly instead of providing it as a callback. So that will not work as you intend.
Typing setTimeout(sayHello, 1000), you're instead providing the function reference instead, to the setTimeout will know what function to execute after the 1000 ms has passed. This is the correct way to do it.

Why does console.log need to be wrapped in a react function [duplicate]

This question already has answers here:
Why I can't pass console.log as a callback argument in Chrome (and Safari)?
(2 answers)
Closed 2 years ago.
Why does console.log need to wrapped before I can use it in an event handler?
https://jsfiddle.net/7e5yLh63/17/
In the example above, this code does nothing (line 44-51) when I click 'Add':
function ManufacturerWrapper() {
return <AddManufacturerForm onAddManufacturer={console.log} />
}
But when I change it to
function log(msg) {
console.log(msg);
}
function ManufacturerWrapper() {
return <AddManufacturerForm onAddManufacturer={log} />
}
It logs to the console as expected. Why can't I just pass in console.log directly? Why do I need to wrap it in my own function first?
I'm using Firefox if that matters
You're probably asking for this:
onAddManufacturer={()=>console.log('log this!')}

Disallowing function to be called from Console in Javascript [duplicate]

This question already has answers here:
How to disable JavaScript function calls from the browser console?
(6 answers)
Closed 3 years ago.
Alright so I've a function (not exactly the one written bellow, that one is just an example)
function test()
{
alert("Hello");
}
Now the problem is that people are able to go into console and just type "test()" and call the function.
Is there a way to prevent this? I dont want of people to call functions from the console.
You can narrow the context that your function is executed in, eg:
(function() {
function test() { /* Do anything */ }
test();
// Call test anywhere here
})()
test(); // Error: test is undefined

how to use recursive setTimeout from within a method [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 8 years ago.
Im trying to use setTimeout() for a recursive function, inside a method. I'm assuming I'm getting something with the 'this' scope wrong though. Here's an example.
(function(scope){
function gamePlay(){
// this.initialize();
}
gamePlay.prototype.droidMovement = function(){
console.log('droid is moving');
this.droidShoot();
}
gamePlay.prototype.droidShoot = function(){
console.log('droidShoot() has been fired');
setTimeout(this.droidShoot,1000);
}
scope.gamePlay = gamePlay;
})(window);
var game = new gamePlay();
game.droidMovement();
Ive tried the setTimeout 3 ways.
//this way repeats the function 2 times then stops
setTimeout(this.droidShoot,1000);
//this gives me an error
setTimeout(function(){this.droidShoot()},1000);
//this fires the function in a loop but doesnt wait the second
setTimeout(this.droidShoot(),1000);
Can anyone please explain what I'm doing wrong? thanks so much in advance.
You may want to remove the setTimeout from within the definition of droidShoot and replace setTimeout(this.droidShoot, 1000) with setInterval(this.droidShoot, 1000.
setInterval will automatically call this.droidShoot repeatedly at a 1s interval

Categories