Method return undefined [duplicate] - javascript

This question already has answers here:
How does "this" keyword work within a function?
(7 answers)
Closed 5 years ago.
I have a problem receiving a value from a void method
For example:
test() {
var x = this.test2("hi there");
console.log(x);
}
test2(data){
return data;
}
I want to receive the data from test2 but it keep saying undefined what do I do wrong in here? And how can I make this work?
It is probably so basic but I just want to know why I receive the value undefined

Add function in front of definition.
function test() {
var x = this.test2("hi there");
console.log(x);
}
function test2(data) {
return data;
}
test();

Try like this :
test(): void {
var x = this.test2("hi there")
console.log(x);
}
test2(data): void {
return data
}

Related

How to reference a sub function from within a function by its name as a string [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 2 years ago.
Can anyone tell me what I can replace the evil eval with in this code?
var x = function(funcName) {
function funcA () {
console.log("a");
}
function funcB () {
console.log("b");
}
var funcToRun = eval(funcName);
return funcToRun();
};
x("funcA");
x("funcB");
I've see a bunch of different solutions, but none seem to fit this scenario. Essentially, I need to be able to pass a string into a function as a "config" of which sub-function to run. I don't have access to the code that calls x, I've just been instructed that only primitive and string values can be configured in the call.
P.S. This is a super simplified version of the actual function.
Thanks!
James
You could just create an object and add those functions as properties that you can then access with a string.
var x = function(funcName) {
function funcA() {
console.log("a");
}
function funcB() {
console.log("b");
}
const fs = {
funcA,
funcB
}
var funcToRun = fs[funcName]
return funcToRun();
};
x("funcA");
x("funcB");

if I assign a function to variable it is showing error in console [duplicate]

This question already has answers here:
Javascript call() & apply() vs bind()?
(24 answers)
Closed 2 years ago.
let user = {
firstName: 'Testname'
}
function testAlert() {
alert(this.firstName);
}
let funcUser = testAlert.call(user); // Testname
funcUser();
Shows error in console:
Uncaught TypeError: funcUser is not a function
I am not getting why it is showing error.
Thanks
Call and apply will run your function, bind just assign context. In your example you should use bind instead of call.
instead of .call use .bind
let user = {
firstName: 'Testname'
}
function testAlert(){
alert(this.firstName);
}
let funcUser = testAlert.bind(user); // Testname
funcUser();
or you can return a function from the testAlert()
example
let user = {
firstName: 'Testname'
}
function testAlert(){
return function() { alert(this.firstName) }
}
let funcUser = testAlert.call(user); // Testname
funcUser()

JavaScript recursion variable value undefined [duplicate]

This question already has answers here:
Recursive function does not return specified value
(2 answers)
Recursive function returns undefined
(3 answers)
Closed 3 years ago.
When I run the below JS, the console correctly logs a as string: "foo", but then the return value is undefined. typeof() also says it's a string. There is no change from line 5 to 6 (the console.log and the return), so how can this spontaneously become undefined?
let a = "foo";
let c = 0;
function test() {
if (c === 5) {
console.log(a);
return a;
} else {
c++;
test();
}
}
In your recursive call, use return test():
let a = "foo";
let c = 0;
function test() {
if (c === 5) {
console.log(a);
return a;
} else {
c++;
return test();
}
}
Explanation: Consider the first call of test() in your original code. If c is 5, it will return a value, but otherwise, it won't return anything. Sure it will execute a second call of test(), but it will not return the value of that call to your main program. This is why the return statement is needed. And the same logic applies to all calls of test() where c is not equal to 5.

Recursion in Javascript ES6 class [duplicate]

This question already has answers here:
How to access the correct `this` inside a callback
(13 answers)
Closed 4 years ago.
I am trying to call the same method within a method in a Javascript ES6 class but it is not working.
class Client {
constructor(connection) {
this.channels = [];
this.nickname = null;
this.user = null;
this.realName = null;
connection.on('data', this.parse_message);
}
parse_message(message) {
let messageObject = {};
if (message.includes('\r\n')) {
message = message.split('\r\n');
message.forEach((el) => {
this.parse_message(el);
});
}
else {
message = message.split(' ');
console.log('Message Received: ', JSON.stringify(message));
}
}
}
When ran I get this error TypeError: this.parse_message is not a function. I tried assigning this to a variable self at the top but that still didn't work.
Pass an arrow function as the bound handler so that you can keep this associated with the method.
connection.on('data', (...args) => this.parse_message(...args));
Now your this in the forEach callback will be the expected value.
You can use bind in the constructor:
this.parse_message = this.parse_message.bind(this);
Bind-ref:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

Function call javascript [duplicate]

This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 6 years ago.
I called helloworld and defined it with below two different ways:
1) With variable
2) With function name itseld
var helloWorld = function() {
return '2';
}
function helloWorld() {
return '1';
}
alert (helloWorld()); // This alert 2, but in absence of "var helloWorld = ....", it alert "1".
Can anyone please explain reason why it's calling var helloWord = ? and not function helloWorld () ?
Thank You !!
why it's calling var helloWord = ? and not function helloWorld () ?
Because functions definitions will be hoisted to the top. And the assignments remains in the same place. So it is getting overridden.
This is how the interpreter sees the code,
function helloWorld() {
return '1';
}
var helloWorld;
//the above function is getting overridden here.
helloWorld = function() {
return '2';
}
alert (helloWorld());

Categories

Resources