admin管理员组文章数量:1289581
I am using a JavaScript library that exposes a constructor as a property of a global object.
In JavaScript, I can call the constructor like this.
var thing = new Library.Thing();
How do I call the constructor in ClojureScript? None of these work.
; These all cause piler errors
(new (.-Thing js/Library)) ; First arg to new must be a symbol
(new (.Thing js/Library))
(new .-Thing js/Library)
(new .Thing js/Library)
(new js/Library/Thing) ; Invalid token: js/Library/Thing
; These all pile to different JS than I am looking for
((.-Thing js/Library).) ; Library.Thing.call(null, _SLASH_);
((.Thing js/Library).) ; Library.Thing().call(null, _SLASH_);
It works fine if I use js* but that's cheating, right?
(js* "new Library.Thing()")
What is the proper way to call a constructor function that is a property of another object?
I am using a JavaScript library that exposes a constructor as a property of a global object.
In JavaScript, I can call the constructor like this.
var thing = new Library.Thing();
How do I call the constructor in ClojureScript? None of these work.
; These all cause piler errors
(new (.-Thing js/Library)) ; First arg to new must be a symbol
(new (.Thing js/Library))
(new .-Thing js/Library)
(new .Thing js/Library)
(new js/Library/Thing) ; Invalid token: js/Library/Thing
; These all pile to different JS than I am looking for
((.-Thing js/Library).) ; Library.Thing.call(null, _SLASH_);
((.Thing js/Library).) ; Library.Thing().call(null, _SLASH_);
It works fine if I use js* but that's cheating, right?
(js* "new Library.Thing()")
What is the proper way to call a constructor function that is a property of another object?
Share Improve this question asked Oct 23, 2013 at 4:03 Josh HeadapohlJosh Headapohl 1952 silver badges7 bronze badges1 Answer
Reset to default 11If you look at http://himera.herokuapp./synonym.html you can find the specific syntax to instantiate objets in clojurescript.
I wrote this js mock library based in this documentation to make a test:
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
return "Hello, " + this.name;
};
var f={
"hola":"hola juan",
Person:Person
};
var person=new f.Person("Juan");
alert(person.greet());
Then from clojurescript you have to use the dot syntax (but prefixing with "js/" your js global type):
(let [Person (.-Person js/f)
juan (Person. "Juan")
]
(.log js/console (.greet juan)))
I don't mention in this answer the :externs property of your cljsbuild pilation beacuse I understand that you are including your js script library directly in your html head document. So, if this line works for you (js* "new Library.Thing()")
it'll mean that the js library is available from the cljs-js-piled.
Anyway, I left an "alert" in the js mock library to check that the file is correctly loaded
I hope it works for you
Juan
本文标签: Calling JavaScript object property as a constructor from ClojureScriptStack Overflow
版权声明:本文标题:Calling JavaScript object property as a constructor from ClojureScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741474920a2380825.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论