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

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

programmeradmin1浏览0评论

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 pact way of creating this mon 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?

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 pact way of creating this mon 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?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Oct 15, 2015 at 22:39 user5441699user5441699 10
  • 4 arr.forEach(function(el, i) { ... }) – Ram Commented Oct 15, 2015 at 22:43
  • 5 No no no, please don't do such things. While you might safe some seconds every time you type this, everyone else working with your code will have to use much more type to find out what the hell you did there. That's terrible style. (Speaking from a C++ point of view, but I would guess that this is just as bad in Java.) – Baum mit Augen Commented Oct 15, 2015 at 22:44
  • 1 I disagree that iterating an array using a for...in loop is dangerous. It's only dangerous if you either don't know how it works, or happen to be using a library / framework which extends the array prototype, which is just stupid and I would much rather find that out sooner than later. – user4639281 Commented Oct 15, 2015 at 22:47
  • 4 A majority of software cost is maintenance which relates to the ability of others to easily read, understand, and modify/maintain the code. Saving a few seconds of typing in an obfuscated way will cost a lot more in the long run. – James Adkison Commented Oct 15, 2015 at 22:59
  • 2 Very good point! Those calculations must be of one of the reasons behind getting fired from "GOOG and MS" :) – Ram Commented Oct 15, 2015 at 23:09
 |  Show 5 more ments

4 Answers 4

Reset to default 5

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/pile 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 remendation 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 piled languages and the #define you are referring to is a pre processor macro, which are used by the preprocessor to aid in piling for platform specific pilation.

As far as I know there are no libraries that solve your problem in JavaScript, but you can check npmjs 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 bee famous
  2. Use the Gcc preprocessor without the piler: gcc-E path/to/fileand 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 ments, 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.

发布评论

评论列表(0)

  1. 暂无评论