0

Object methods in JavaScript.

where is this undefined thing coming from in my output? code: https://code.sololearn.com/WxmpE3RPBFJP/#js output: how are you doingundefined

16th Sep 2018, 6:38 PM
Kshitij Shukla
Kshitij Shukla - avatar
32 Answers
16th Sep 2018, 6:49 PM
josh mizzi
josh mizzi - avatar
+ 1
you can check below link code where i have mention how your program works and give you a undefined in string https://code.sololearn.com/WzAqw1nYF7ok/?ref=app
16th Sep 2018, 6:50 PM
Kirtan
Kirtan - avatar
+ 1
output is hi
16th Sep 2018, 7:07 PM
Kirtan
Kirtan - avatar
+ 1
okay i understand this what you wrote. but what i don't understand is, if function a() { document.write(31);} var a = 'hi'; //as you said var b = a; what will get stored in b? function definition or variable a
16th Sep 2018, 7:32 PM
Kshitij Shukla
Kshitij Shukla - avatar
+ 1
whereas in this code i get an error that says a is not an function which clearly It is. function a() { return 31;} var a = 'hi'; //as you said var b = a(); document.write(b);
16th Sep 2018, 7:34 PM
Kshitij Shukla
Kshitij Shukla - avatar
+ 1
but what about the second code, I'm clearly assigning var b = a(); which is a function. still i get an error which days a ain't a function
16th Sep 2018, 7:37 PM
Kshitij Shukla
Kshitij Shukla - avatar
+ 1
function a(){return 31;} //var a = 'hi';//if you uncomment this line means you override the a in string from the function so a() gives error var b = a; document.write(b);
16th Sep 2018, 7:39 PM
Kirtan
Kirtan - avatar
+ 1
okay, so because var a comes after function a it overrides?
17th Sep 2018, 7:00 AM
Kshitij Shukla
Kshitij Shukla - avatar
0
Where you have "{greet:hello()..." "hello()" does not equal "hello". "hello()" = the return value of the function hello. As "hello()" does not return a value it will return undefined.
16th Sep 2018, 6:44 PM
josh mizzi
josh mizzi - avatar
0
so basically you have to return basically something, and since nothing is getting returned, I'm getting undefined?
16th Sep 2018, 6:47 PM
Kshitij Shukla
Kshitij Shukla - avatar
0
okay so Josh, i understand you did not put the function hello() in object as hello, you put it without braces, and in the method you put braces, although i don't understand why, but understand how i should proceed with the code.
16th Sep 2018, 6:54 PM
Kshitij Shukla
Kshitij Shukla - avatar
0
kirtan, still, there's an alert which gives undefined.
16th Sep 2018, 6:54 PM
Kshitij Shukla
Kshitij Shukla - avatar
0
please uncomment return statement and check again
16th Sep 2018, 6:56 PM
Kirtan
Kirtan - avatar
0
your hello function returns nothing that's why
16th Sep 2018, 6:57 PM
Kirtan
Kirtan - avatar
0
is it necessary to return something everytime in a function?
16th Sep 2018, 6:59 PM
Kshitij Shukla
Kshitij Shukla - avatar
0
also if i uncomment that return statement, i get an unexpected Identifier error
16th Sep 2018, 7:00 PM
Kshitij Shukla
Kshitij Shukla - avatar
0
can you review my code first
16th Sep 2018, 7:02 PM
Kirtan
Kirtan - avatar
0
where is this undefined thing coming from in my output ? <-- your question is this. that's why i commented return statement in my code. :)☺️
16th Sep 2018, 7:04 PM
Kirtan
Kirtan - avatar
0
// Created by Kshitij Shukla function hello() { document.write("how are you doing"); return 'hi'; } var hi={greet:hello()} var x = hi.greet; // hello() called here and function return nothing to in x that's why undefined coming.. alert(x); document.write(hi.greet);
16th Sep 2018, 7:06 PM
Kirtan
Kirtan - avatar
0
okay i understood your code, i also understood how it works, can you please tell me how is it different from what josh did in the code he sent above? he put the () in method of the object and not the property's value
16th Sep 2018, 7:07 PM
Kshitij Shukla
Kshitij Shukla - avatar