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
2 Answers
Reset to default 4As 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
版权声明:本文标题:What is a JavaScript Macro? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743946997a2566588.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论