Реализация String.prototype.startsWith в ClojureScript - PullRequest
0 голосов
/ 28 мая 2019

Мне нужно преобразовать этот код Javascript в ClojureScript.У меня возникли проблемы с ключевым словом this.

Вот мой JS:

if (!String.prototype.startsWith) {
  String.prototype.startsWith = function(searchString, position) {
    position = position || 0;
    return this.indexOf(searchString, position) === position;
  };
}

Я получаю ошибку Object doesn't support property or method 'startsWith' с компилятором Google Closure в расширенном режиме, поэтомуМне нужно добавить этот код.(IE 11)

Ответы [ 2 ]

5 голосов
/ 28 мая 2019

ClojureScript уже имеет clojure.string/starts-with?:

$ 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

повторно найти также может помочь вам найти его.

1 голос
/ 28 мая 2019

Попробуйте этот код 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))))
...