ClojureScript:如何通过原型将方法添加到JS对象? [英] ClojureScript: How to add method via prototype to JS Object?

查看:157
本文介绍了ClojureScript:如何通过原型将方法添加到JS对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为现有的JavaScript系统添加一些功能。然后再次从JavaScript使用(而不是在ClojureScript命名空间内)。也许这是不可能的。

I'm trying to add some functionality to an existing JavaScript system. To be then used from JavaScript again (as opposed to within the ClojureScript namespace). Perhaps this isn't possible?

这里是我想做的简化:

// JavaScript
String.prototype.foo = function() {
  return "bar";
}

# CoffeeScript
String::foo = ->
  "bar"



我希望能够运行上面的脚本,然后调用

I want to be able to run my script above, and then call it from elsewhere in the code.

我试着用 extend-type defprotocol 以及 export ,但似乎没有公开我的foo函数。

I've tried messing with extend-type and defprotocol, along with export, but nothing seemed to expose my foo function.

这可能是一个设计决定,ClojureScript不适合我在这里,但我只是想确保我不会忽视的东西。 / p>

It's possible that this was a design decision and ClojureScript isn't going to work for me here, but I just wanted to make sure I wasn't overlooking something.

推荐答案

可以这样做:

(set! (.-foo (.-prototype js/String)) (fn [] "bar"))

或者你可以使用.. sugar:

Or you can use .. sugar:

(set! (.. js/String -prototype -foo) (fn [] "bar"))

这篇关于ClojureScript:如何通过原型将方法添加到JS对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆