Javascript pointer to function [duplicate] - javascript

This question already has an answer here:
Illegal Invocation error when console.log passed in a function
(1 answer)
Closed 8 years ago.
Javascript is def not my main programming language I for practice I wanted to do something simple, at least i thought ;)
This is my code:
function Log () {}
Log.format = function (func, facility, text) {
func("hellow");
};
Log.debug = function(facility, text) {
Log.format(console.warn);
};
When I call Log.debug("we", "deb"); I get a Uncaught TypeError: Illegal invocation error. What am I doing wrong?

Depending on the browser, console methods can only be called in the context of console. Try Log.format(console.warn.bind(console));

Related

How do I call a JavaScript function from a website's source? [duplicate]

This question already has answers here:
Is it possible to gain access to the closure of a function?
(4 answers)
Stop execution of Javascript function (client side) or tweak it
(5 answers)
Closed last year.
There exists a quiz website that requires the submission of an answer before the correct one is revealed. I did some digging into the website's JavaScript and found this function:
function getAnswer(curCard) {
var answer = void 0;
if (curCard.testTerm) {
answer = curCard.testTerm;
} else {
answer = curCard.choices[curCard.correctAnswerIndex];
}
return answer;
}
I want to use the Chrome developer console to invoke this function but I am met with this error:
VM3133:1 Uncaught ReferenceError: getAnswer is not defined
at <anonymous>:1:1
This function is deep within the webpage's source and the .js file itself is like 10k lines of code. I am 99% sure this is a scoping issue and do not know how to get into the proper scope so that I can call this function.

When I try to use ``` ...``` decorator doesn't work [duplicate]

This question already has answers here:
ECMAScript 6 arrow function that returns an object
(6 answers)
Closed 3 years ago.
I am trying to do this :
setMyState(prevState=> {...prevState, name: e.nativeEvent.text });
While the console says src/components/addItem.js: Unexpected token
And it doesn't work :(
While using a js file .. I tried with .jsx too and same error :(.
Also I found an answer here WebStorm error: expression statement is not assignment or call
but it didn't solve my problem since when I run the app now it crashes exactly there ...
If you use an arrow function and want to return an object you need to wrap your object with (). If you don't use, arrow function thinks {} is the body block. So, try using:
setMyState(prevState=> ({...prevState, name: e.nativeEvent.text }));

TypeError: Cannot read property "messageMetaData" from undefined [duplicate]

This question already has answers here:
How can I test a trigger function in GAS?
(4 answers)
Closed 11 months ago.
I am writing AppScript to develop an Add On to allow user to move the current thread under a label. For the purpose of fetching current thread id.
BuildAddOn(e)
function buildAddOn(e) {
// Activate temporary Gmail add-on scopes.
var accessToken = e.messageMetaData.accessToken;
GmailApp.setCurrentMessageAccessToken(e.messageMetadata.accessToken);
var messageId = e.messageMetadata.messageId;
While I try to run the function, the following error comes:
TypeError: Cannot read property "messageMetaData" from undefined.
(line 3, file "Code")
Calling the buildAddOn somewhere else in your code will cause the error. In this context, e is an object that will be passed only during initialization. And this is not equivalent to the object that you see in action handlers.
Sorry, if I misunderstood your question.

Javascript 'not defined' error when declaring default function parameter matching to a constant? [duplicate]

This question already has an answer here:
Scope of Default function parameters in javascript
(1 answer)
Closed 4 years ago.
Just discovered some weird error that reads:
Uncaught ReferenceError: symbol is not defined
Code in question:
const symbol='tNEOUSD';
function get_position(symbol=symbol){
console.log(symbol);
}
get_position();
How come it's not defined? That's a really weird!
On the other hand if I use a different parameter name it works just fine:
const symbol='tNEOUSD';
function get_position(sym=symbol){
console.log(sym);
}
get_position();
Does anyone know why this happens?
You cant use a variable which is already declared outside as a parameter.

Why does setTimeout(location.reload) throw a TypeError? [duplicate]

This question already has answers here:
Why can't I pass "window.location.reload" as an argument to setTimeout?
(4 answers)
Closed 6 years ago.
I'm trying to understand the strange behavior of this code:
window.setTimeout(window.location.reload, 200);
In Firefox this throws a TypeError:
TypeError: 'reload' called on an object that does not implement interface Location.
In Chromium this throws another TypeError:
Uncaught TypeError: Illegal invocation
These two alternatives work fine:
window.setTimeout('window.location.reload()', 200);
window.setTimeout(function(){window.location.reload()}, 200)
Why?
This is due to the fact that window.location.reload will be called out of context, so the actual function reload() does not have any reference to the location object.
In JavaScript, if you call a function foo.bar(), the context is foo, so this refers to foo within the function bar.
However, if you assign var a = foo.bar and then call a() (which is still the same function), it will have no context, so this will be undefined. This is what happens when you pass the function as a method parameter.
The usual way to work around this issue is to bind the context:
window.setTimeout(window.location.reload.bind(window.location), 200);
This ensures that the function will always be called in the context of window.location, no matter how it is called.
There is a nice example in the Mozilla docs.

Categories

Resources