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?
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
You are not passing parameters ,other than your code looks good. what's your actual expectation??
– Code-EZ
Sep 11 '18 at 20:52