简体   繁体   English

JavaScript Function 类似变量

[英]JavaScript Function like Variable

I want to use this function to call between results:我想用这个 function 在结果之间调用:

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') res.send('Blah Blah Blah'+flag2('My_Flag')+' Blah Blah Blah')

Expected Result:预期结果:

Blah Blah Blah My_Flag Blah Blah Blah Blah Blah Blah My_Flag Blah Blah Blah

This Question is based on Expressjs, but others might be able to help too这个问题基于 Expressjs,但其他人也可以提供帮助

PS: This is for CTF or capture the flag nothing about countries these will be diffrent and unique everywhere thats why im using functions... PS:这是针对 CTF 或捕获国旗的,与国家无关,这些在任何地方都是不同的和独特的,这就是我使用函数的原因......

In your second example 'This is '+newflag+' Got it?'在您的第二个示例'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 ).它不会将它传递给另一个 function (如console.logres.send )。

It doesn't assign it to a variable.它不会将其分配给变量。

It doesn't assign it to a property of an object.它不会将其分配给 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') ).它不会return它,因此可以在flag2('My_Flag')的左侧使用(并不是说您在该 function 调用的左侧有任何内容……尽管您在后面的示例中这样做: 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太简单了,忘记返回了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 行为类似于函数的 JavaScript 变量 - JavaScript variable that behaves like a function JavaScript调用变量作为对象和函数,如jQuery - JavaScript call variable as Object and Function like jQuery D3示例:看起来像一个javascript变量,但被称为一个函数 - D3 example: looks like a javascript variable, but is called like a function Javascript:在递归函数中初始化一次变量(如静态变量) - Javascript: initializing a variable once (like a static variable) in a recursive function 将可变数量的参数传递给看起来像“function(*x)”的 function JavaScript - Passing in variable amount of parameters to a function that looks like “function(*x)” JavaScript 使用冒号,分号等符号将变量传递给javascript函数 - Passing variable to javascript function with symbols like colon, semicolon, etc 看起来像Array函数的JavaScript语法:[variable]({key},arg){} - JavaScript syntax which looks like Array function: [variable] ({ key }, arg) { } 可能通过这样的形式将变量从html形式传递给javascript函数? - possible to pass variable to javascript function from html form like this? Javascript:如何创建既像对象又像函数的变量? - Javascript: How to create a variable that acts like both an object and a function? 像JavaScript中的警报功能 - alert like function in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM