最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

header files - use .h defines in javascript - Stack Overflow

programmeradmin6浏览0评论

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
Add a ment  | 

4 Answers 4

Reset to default 6

JavaScript 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

发布评论

评论列表(0)

  1. 暂无评论