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

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

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, Stop function from logging to console on call [duplicate]

This question already has answers here:
How to quickly and conveniently disable all console.log statements in my code?
(38 answers)
Closed 1 year ago.
Say I have a function imported from a library
import doSomething from 'someLibrary';
And doSomething has a console.log() call inside of it. So, when I call do something
doSomething();
Some message is logged to the console. How do I call the function doSomething without it logging anything to the console?
Try this:
{
let console = {log: ()=>{}};
doSomething();
}

Unexpected behaviour calling function from function [duplicate]

This question already has answers here:
What is the difference between a function call and function reference?
(6 answers)
Closed 2 years ago.
Recently I started learning about JS, and I got stuck in the following issue:
I am trying to call a function "draw_point" when a "mousemove" event happens.
When trying the following, the code works as expected (the function gets called):
svg.on('mousemove', draw_point(true));
But when replacing the caller function like this, it stops working and I don't get any error messages to troubelshoot.
svg.on('mousemove', function () {
draw_point;
});
Any explanation of what is going on?
Both examples are wrong, but for different reasons. In the first, you're calling the functiondraw_point immediately and passing its return value to the .on call. In the second, you're creating an anonymous function that wraps draw_point, but you're not calling draw_point. This is legal because javascript allows empty statements like myVar; or 3;.
What you want to do is pass the function draw_point, but not call it. It will be called when the handler runs:
svg.on('mousemove', draw_point);

(function(symbolName) { - What does this mean [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What do parentheses surrounding a JavaScript object/function/class declaration mean?
What does (function($) {})(jQuery); mean?
I am trying to understand how Edge works so I can use my own code,
I have not come accross this before, but what does this mean:
(function(symbolName) {
//CODE
})("stage");
It's an anonymous function that is defined and then called with the argument "stage"
It is the similar to doing:
var myfunc = (function (symbolName) {
//CODE
});
myfunc("stage");
OR
function myfunc(symbolName) {
//CODE
}
myfunc("stage");
except that when the function is defined in either of these ways it will be 'hoisted' to the top of the block scope - but thats a whole other topic.
In Javascript you can ddefine anonymous functions by simply typing:
(function(){alert("Hello")}); /* ok, this do nothing, but it is correct */
It is also possible to call a function directly:
(function(){alert("Hello")})(); /* alert is displayed */
If the function has arguments, you have to specify the arguments:
(function(args){alert(args)})("Hello"); /* alert is displayed with the passed arguments */
I suggest you this tutorial.

What this mean in javascript? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Explain JavaScript's encapsulated anonymous function syntax
I have just read a javascript book but I have seen this code:
1(function() {
// code
})();
what is this ? is a special function ?
As written, it has a syntax error.
I'm guessing it was more like:
(function() {
// code
})();
or
(function() {
// code
}
)();
Break it down:
(FOO)() // calls FOO with no arguments.
And
function() { //creates a function that takes no arguments.
// code
}
Hence together it would create a function that takes no arguments, and then call it. I can't see why you would apart from just showing that you can.
It looks like the intent was to declare the function inline/anonymous and immediately execute it.

Categories