Object methods in JavaScript. | SoloLearn: Learn to code for FREE!

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

9/16/2018 6:38:39 PM

Kshitij Shukla

32 Answers

New Answer

+1

Try this. https://code.sololearn.com/WT3vC1k19xw8/?ref=app

+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

+1

output is hi

+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

+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);

+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

+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);

+1

okay, so because var a comes after function a it overrides?

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.

0

so basically you have to return basically something, and since nothing is getting returned, I'm getting undefined?

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.

0

kirtan, still, there's an alert which gives undefined.

0

please uncomment return statement and check again

0

your hello function returns nothing that's why

0

is it necessary to return something everytime in a function?

0

also if i uncomment that return statement, i get an unexpected Identifier error

0

can you review my code first

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. :)☺️

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);

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