admin管理员组

文章数量:1355540

Recently I have seen people talk about the using of macros in JavaScript. I have no idea what that means and after looking up documentation on MDN I came up without any answer. So that leads me to my question …

What are JavaScript macros?
How/why are they used?
Is it a form of meta-programming?

Answers with examples and example code would be appreciated greatly.

Recently I have seen people talk about the using of macros in JavaScript. I have no idea what that means and after looking up documentation on MDN I came up without any answer. So that leads me to my question …

What are JavaScript macros?
How/why are they used?
Is it a form of meta-programming?

Answers with examples and example code would be appreciated greatly.

Share Improve this question edited May 27, 2022 at 0:33 Ouroborus 16.9k8 gold badges40 silver badges65 bronze badges asked Jan 9, 2014 at 18:58 user3084728user3084728 6052 gold badges9 silver badges16 bronze badges 5
  • Maybe this? sweetjs – m59 Commented Jan 9, 2014 at 18:59
  • 1 It's this: sweetjs. It's new and people are starting to talk about it :) – gen_Eric Commented Jan 9, 2014 at 19:00
  • Presumable the same thing as macros in any other language. (Which admittedly is a pretty wide range, from C preprocessor macros to simple lisp macro systems to modern fully hygenic phase-separated macro systems). – user395760 Commented Jan 9, 2014 at 19:02
  • Guys, thank you for the ments but these are not constructive answers. Please feel free to leave a solid answer and i'm happy to donate upboats to you. – user3084728 Commented Jan 9, 2014 at 19:08
  • possible duplicate of How can I simulate macros in JavaScript? – Anderson Green Commented Sep 28, 2014 at 3:54
Add a ment  | 

2 Answers 2

Reset to default 4

As has been posted in ments it's a macro system for javascript.

It's a way of defining text replacements in the "pre-processing phase". So you would define a macro and use it in your code, then run them both through sweet.js and the output would be code with text replacements.

example:

macro swap {
  rule { ($a, $b) } => {
    var tmp = $a;
    $a = $b;
    $b = tmp;
  }
}

var a = 10;
var b = 20;

swap (a, b)

After running this through sweet.js we get the expanded code:

var a$1 = 10;
var b$2 = 20;

var tmp$3 = a$1;
a$1 = b$2;
b$2 = tmp$3; 

I think the use case for this is more centered around frameworks and the likes. It's a more flexible way of saving lines of code than a function.

In JS, a function can basically either

  • Mathematically calculate something based off of it's inputs
  • Perform some kind of side effect on another variable within the scope of the function

But Macros are more flexible, it's like code that just takes in inputs and actually equates to text that can be piled as regular code. It's a mon feature in languages that prioritize code prettiness over code-traceability. A couple of notable examples are Ruby, Rust, and Elixir.

Here's an example of a ruby macro and what the equivalent code would look like in js.

In ruby you can do this to tell a class to have certain relationship methods in it's ORM

class Movie < ActiveRecord::Base
  has_many :reviews
end

In this case, saying has_many :reviews dumps a bunch of methods onto the Movie class. So because of that line you can call movie.reviews.

In TypeORM right now you could do something like

class Movie {
    @OneToMany(() => Review, review => review.movie)
    reviews: Review[];
}

If macros made it into js, you could clean this up to look more like

class Movie {
   oneToMany(Review, "reviews", "movie")
}

My guess is that this won't happen any time soon. IMO one of the things you lose with macros, is that it bees less clear what your code actually does, I also have to imagine it would be a pretty big change for linters and type checkers. There's also other ways of dumping a bunch of functionality into an object or function.

For example in react hook form you can acplish something similar using closures and spreading.

<input {...register("firstName")} placeholder="Bill" />

本文标签: What is a JavaScript MacroStack Overflow