简体   繁体   中英

JavaScript Function like Variable

I want to use this function to call between results:

Working Result:

function flag1(newflag){
    console.log('This is'+newflag+' Got it?')
}

flag1('My_Flag')

// Result: This is My Flag Got it?

Not showing anything / Not Working:

function flag2(newflag){
    'This is '+newflag+' Got it?'
}

flag2('My_Flag')

// Result: <Nothing>

I know 2nd Example is Dumb but what else can i think of? My Goal is to send this in response with other stuff like above: This is My Flag Got it?

For example:

res.send('Blah Blah Blah '+flag2('My_Flag')+' Blah Blah Blah')

Expected Result:

Blah Blah Blah My_Flag Blah Blah Blah

This Question is based on Expressjs, but others might be able to help too

PS: This is for CTF or capture the flag nothing about countries these will be diffrent and unique everywhere thats why im using functions...

In your second example 'This is '+newflag+' Got it?' creates a string.

That is all it does.

It doesn't pass it to another function (like console.log or res.send ).

It doesn't assign it to a variable.

It doesn't assign it to a property of an object.

It doesn't return it so it can be used on the left hand side of flag2('My_Flag') (not that you have anything on the left hand side of that function call … although you do in the later example: res.send('Blah Blah Blah '+flag2('My_Flag')+' Blah Blah Blah') ).

Since you don't do anything, the string is thrown away immediately after it is created (unless the compiler optimises the string creation away entirely).


If you want to do something with the string, then you have to write code which does that.

This Worked:

function flag2(newflag){
   return (newflag);      
}

res.send('Blah Blah Blah '+flag2('My_Flag')+' Blah Blah Blah')

Its so simple, forgot return

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM