简体   繁体   English

如何使用^:export标记用`reify`创建的方法,以便Closure编译器不重命名它们?

[英]How can I mark methods created with `reify` with ^:export, so that the Closure compiler doesn't rename them?

When creating JavaScript objects with reify , how can I mark the methods with ^:export so that the Google Closure compiler doesn't rename them in advanced mode? 使用reify创建JavaScript对象时,如何使用^:export标记方法,以便Google Closure编译器不会在高级模式下重命名它们?

For example: 例如:

(reify
   Object
   (foo [this] ...)
   (bar [this] ...))

I've tried 我试过了

(reify
   Object
   (^:export foo [this] ...)
   (^:export bar [this] ...))

but this doesn't seem to help, and the names still get changed with advanced optimizations. 但这似乎没有帮助,并且名称仍然会随着高级优化而改变。

If there isn't a way to do this, how can I construct a JavaScript object with methods, other than creating a plain js-obj and using set! 如果没有办法做到这一点,我怎么能用方法构造一个JavaScript对象,而不是创建一个普通的js-obj并使用set! to set functions to properties (where I'm not sure how to prevent advanced optimizations from breaking things either)? 将函数设置为属性(我不知道如何防止高级优化破坏事物)?

You have to provide ^:export on your protocol methods as you will call them in JS, not methods from your reified object directly. 您必须在协议方法上提供^:export ,因为您将在JS中调用它们,而不是直接从您的实现对象中调用方法。

(ns example.core)

(defprotocol MyProtocol
  (^:export foo [this])

(defn ^:export create []
  (reify
    MyProtocol
    (foo [this] "bar")))

Then you can use it from JS: 然后你可以在JS中使用它:

var a = example.core.create();
var b = example.core.foo(a);
// b = "bar"

I tried it with the current cljs.jar and it emitted optimized JS with original foo name. 我用当前的cljs.jar尝试了它,它发出了原始foo名称的优化JS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过Google Closure Compiler将功能标记为“私有”以重命名? - How i can mark function as “private” to renaming it by Google Closure Compiler? 如何在ADVANCED_OPTIMIZATIONS中的Closure Compiler中导出公共类方法? - How to export public class methods in Closure Compiler in ADVANCED_OPTIMIZATIONS? 闭包编译器导出所有原型和静态方法 - Closure compiler export all prototypes & static methods 我如何解构反应查询异步返回变量,以便其中之一不会记录未定义? - How can I destructure react query async return variables so that one of them doesn't log undefined? 即使使用对象,如何强制Google Closure Compiler重命名方法 - How to force Google Closure Compiler to rename methods even when using objects 闭包如何实现数据封装? - How does closure reify the data encapsulation? Closure编译器不会删除未使用的属性 - Closure compiler doesn't remove unused properties 闭包编译器-保留未使用的函数,并且不要重命名未定义的函数 - closure compiler - keep unused functions and don't rename the undefined 如何封装对象属性,以便不接管以前的对象? (原型/关闭)。 - How can I encapsulate object property so that previous object isn't taken over? (Prototypes/closure.) 如何为Closure编译器设置language_in选项? - How can I set the language_in option for the Closure compiler?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM