admin管理员组文章数量:1322021
I want to use defines in .h files in my JavaScript.
I have input from user HTML file, and want to show them in the HTML file like:
input = 5
the .h file contains define:
#define CODE 5
I want to show to the user the output: CODE
I want to use defines in .h files in my JavaScript.
I have input from user HTML file, and want to show them in the HTML file like:
input = 5
the .h file contains define:
#define CODE 5
I want to show to the user the output: CODE
Share Improve this question edited Feb 22, 2021 at 18:59 Christos Lytras 37.3k4 gold badges85 silver badges117 bronze badges asked Nov 28, 2012 at 11:09 user1805755user1805755 251 silver badge5 bronze badges 1- 4 You can't mix JavaScript with C! – bfavaretto Commented Nov 28, 2012 at 11:14
4 Answers
Reset to default 6JavaScript does not have .h files. It is not C/C++.
You could achieve this by using an object map / associative object:
map = {
1: "FISH",
2: "CHIPS",
3: "EAT",
4: "SLEEP",
5: "CODE",
}
input = 5
alert(map[5]) // Will alert CODE, as required.
In fact you can use C preprocessor (gpp for example) for js-file. But C preprocessor doesn't work as you have described. It will replace «CODE» with «5».
So if you want replace exactly number, you must use it as index of object or array. Otherwise you also can use variable or even preprocessor.
You can't replace simple number with string.
JavaScript does not support *.h files
You can load header file with defined constanst:
header.js:
var CODE = 5;
and evalute it in main code
main.js:
var code = $.get("header.js")
eval(code.responseText);
there is no .h files for making your own programing language or functions but you can make functions that make your own functions or mands
here's a way to do it: https://www.guru99./learn-functions-in-javascript-in-5-minutes.html
本文标签: header filesuse h defines in javascriptStack Overflow
版权声明:本文标题:header files - use .h defines in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742110452a2421230.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论