Javascript ClojureScript中String.prototype.startsWith的实现
我需要将此Javascript代码转换为ClojureScript。我在使用这个关键字时遇到了一些问题 以下是我的JS:Javascript ClojureScript中String.prototype.startsWith的实现,javascript,object,clojurescript,Javascript,Object,Clojurescript,我需要将此Javascript代码转换为ClojureScript。我在使用这个关键字时遇到了一些问题 以下是我的JS: if (!String.prototype.startsWith) { String.prototype.startsWith = function(searchString, position) { position = position || 0; return this.indexOf(searchString, position) === posi
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
};
}
我得到一个对象不支持属性或方法“startsWith”,Google闭包编译器在高级模式下出错,所以我需要添加此代码。IE 11试试这个ClojureScript代码:
(when (not (.. js/String -prototype -startsWith))
(set!
(.. js/String -prototype -startsWith)
(fn [searchString position]
(set! position (or position 0))
(= (this-as this (.indexOf this) searchString position) position))))
ClojureScript已包含clojure.string/以?开头:
也可以帮你找到它。什么样的麻烦?你试过什么?这有什么问题?用法看起来是正确的。但更重要的是:您应该使用实际的polyfill,而不仅仅是一个执行类似于规范的操作的函数。或者至少从中取一个。在Google Closure compiler Prevance模式后,我发现Object不支持属性或方法“startsWith”错误。您使用的是哪个版本的CLJS?你能在要点中发布一个最小的修改吗?1.10.520,但我只在IE 11上得到这个错误。
$ clj -Sdeps '{:deps {org.clojure/clojurescript {:mvn/version "RELEASE"}}}' -m cljs.main -re node
ClojureScript 1.10.520
cljs.user=> (require '[clojure.string :as str])
nil
cljs.user=> (str/starts-with? "foobar" "foo")
true