简体   繁体   中英

What is the Javascript equivalent to C++'s #define?

For-each loops in JS are "dangerous" to use on arrays, and I can't simply do

for (var idx in arr)

and instead have to do

for (var idx = 0, len = arr.length; idx < len; ++idx)

which is very laborious to type. Suppose it takes 3 seconds to type and I have to type it 10,000 times in my life ...

3s x 10,000 / 60h = 500h

500h * $28/h = $14,000

It would be better to have a compact way of creating this common line of code. It would be nice to have some preprocessor directive like

#define L(arr,idx,len) for (var idx = 0, len = arr.length; idx < len; ++idx)

and then I could just write stuff like

var myArray = [1, 69, 193912];
L(myArray,k,n)
{
   // ... 
}

Is this possible?

Javascript does not have a preprocessor like C/C++ so there is no direct equivalent to C's #define in Javascript.

The only pseudo replacement that is built into the language is to use an actual function rather than a preprocessor expansion which is executed at runtime, not at parse/compile time.

So, your options are:

  1. Use the regular for (var i = 0; i < arr.length; i++) loop that everyone knows and understands.
  2. Use .forEach() , the built-in array iterator
  3. Create your own function that will do the iteration (no real point when .forEach() is already there).
  4. Use the ES6 of as in for (let i of arr) . You can either use this directly in an ES6 supported environment or you can use a transpiler like Babel.
  5. Use a preprocessing step in your build system so you can actually add preprocessing to your JS files and have something like #define.

A safe recommendation would be to use .forEach() and add a polyfill if versions of IE before IE9 are required or go with the ES6/transpiler option (which has many other benefits too).

C and C++ are compiled languages and the #define you are referring to is a pre processor macro, which are used by the preprocessor to aid in compiling for platform specific compilation.

As far as I know there are no libraries that solve your problem in JavaScript, but you can check npmjs.org because I may be wrong.

Nevertheless, I have three ideas for you:

  1. Writing your own preprocessor to your liking and then just using it as part of your build normal js build routine. Then upload it to npm and become famous
  2. Use the Gcc preprocessor without the compiler: gcc-E path/to/file and directing its output to a file, which can also be part of your js build routine. A ridiculous example:

     #define FOR(s,end) for(i=s;i<=end;i++) #define P console.log #define func function func add(a,x){ P(a+x) } FOR(1,10){ add(i,i) }
  3. Write your own functions to make life easier. Instead using loop syntax, write functions like loop( start, finish, callback) . But even this is the same amount of writing. I would look at options 1 and 2.

But like many of the comments, doing this is Js can cause confusion and waste much more time for others reading your code in the future.

in C:

   #define max(a,b) (a>b ? a : b)
   to use it:
   int result = max(2,3);

in JS:

   let max = (a,b) => a>b ? a : b; 
   so use it:
   let result = max(2,3);

Not really the same, but you can define your own function like that one:

function myFor(arr, fn) {
    for(var i = 0; I < arr.length; i++) {
        fn(i, arr[i]);
    }
}

Thus you can use it as it follows:

myFor(myArr, myForEachFn);

Where the second argument is a function you want to apply on each element. Besides the long names I've used, it's faster to type than the for loop above.

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