How to get the value returned by a function in Typescript?

How to get the value returned by a function in Typescript?



I have simply functions to set new subject and mark:


public setSubject(subject)
return subject;


public setMark(mark)
return mark;



I want setSubject() to return a string with the subject name and setMark() to return a number. I call them in another function matFn():


setSubject()


setMark()


matFn()


public matFn()
var subject="Maths";
this.setSubject(subject);



For every subject I have function which gets subject name and call setSubject() with this subject as an argument.
In another function getData() I want to display them (next step will be the functionality, but let's start with console.log):


setSubject()


getData()


public getData()
var subject = this.setSubject();
var mark = this.setMark();
console.log(subject)
console.log(mark)



I want to display exactly what setSubject() and setMark() returns. How would I do that?






You are not passing parameters ,other than your code looks good. what's your actual expectation??

– Code-EZ
Sep 11 '18 at 20:52





1 Answer
1



If you are looking for getter and setter , you can try like below


class SomeClass
private _subject:string = "";
get subject():string
return this._subject;

set subject(value:string)
this._subject = _subject;



let someClassObj= new SomeClass();
obj.subject="hellow"



You can read the same


console.log(obj.subject)



Another Approach



declare a variable to set the value , you can read later when need


subject:string="";
public setSubject(subject)
this.subject=subject;



if the method is in same class you can access with this object on your method want to access


this


console.log(this.subject)



Else you have to create an instance of the class.


let subjectClass=new Subject();
console.log(subjectClass.subject)



Sample Code
https://stackblitz.com/edit/typescript-fr8jro?file=SubjectClass.ts






I would do it if I had subject hard coded but I need to set it with matFn(). I think that matFn() could return subject string but still I don't know how to get value returned by a function. P.S. I edited my question, please take a look.

– mikolaj
Sep 11 '18 at 21:09







@mikolaj the return value will be the same value as a argument in the function. you don't need to call a value returned by a function. just simply log console.log(this.setSubject('maths')) , it will output 'maths'

– Code-EZ
Sep 11 '18 at 21:18






@mikolaj In every programming language it's like that , I think you are missing something. Please clear your thoughts and update the question please

– Code-EZ
Sep 11 '18 at 21:21






So I do this in matFn() but I need access to it from getData()

– mikolaj
Sep 11 '18 at 21:22






@mikolaj now I think I got your requirement , you want to read the subject you set in another function I am I right??

– Code-EZ
Sep 11 '18 at 21:25



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ