将JavaScript对象转换为ClojureScript:Getter和Setter Properites

时间:2016-01-30 18:40:06

标签: javascript clojure clojurescript

我正在努力解决以下问题:

通常JS对象通过js-> clj转换为ClojureScript。 这适用于原型Object的对象。 对于我正在使用的其他人:

(defn jsx->clj [o]
  (reduce (fn [m v] (assoc m (keyword v) (aget o v)))  {} (.keys js/Object o)))

我发现,这些操作无法改变幕后获取功能的“属性”。

有人有这方面的经验吗?

1 个答案:

答案 0 :(得分:1)

当你说“幕后的getter功能无法通过这些操作转换”时,我不确定你的意思。

其中一个问题是,当您将JS对象转换为CLJS映射时,getter和setter将不会绑定到原始JS对象,因此它们将无法访问此对象的属性。

请考虑以下代码:

;; This is your function, no changes here.
(defn jsx->clj [o]
  (reduce (fn [m v] (assoc m (keyword v) (aget o v)))  {} (.keys js/Object o)))


;; This is an improved version that would handle JS functions that
;; are properties of 'o' in a specific way - by binding them to the
;; 'o' before assoc'ing to the result map.
(defn jsx->clj2 [o]
  (reduce (fn [m v]
            (let [val (aget o v)]
              (if (= "function" (goog/typeOf val))
                (assoc m (keyword v) (.bind val o))
                (assoc m (keyword v) val))))
          {} (.keys js/Object o)))

;; We create two JS objects, identical but distinct. Then we convert
;; both to CLJS, once using your function, and once using the improved
;; one.
;; Then we print results of accessing getter using different methods.
(let [construct-js (fn [] (js* "new (function() { var privProp = 5; this.pubProp = 9; this.getter = function(x) { return privProp + this.pubProp + x; }; })"))
      js-1 (construct-js)
      js-2 (construct-js)
      clj-1 (jsx->clj js-1)
      clj-2 (jsx->clj2 js-2)]
  (.log js/console "CLJS objects: " (.getter js-1 10) ((:getter clj-1) 10) (.getter js-2 10) ((:getter clj-2) 10)))

打印:

CLJS objects:  24 NaN 24 24

这意味着以下代码:((:getter clj-1) 10)失败,而((:getter clj-2) 10)按预期工作。第二个工作,因为.bind()已被用于正确地将函数与JS对象联系起来。

这个问题,如果那确实是你失败了,那相当于JS中有时会犯下的错误:

var Constructor = function() {
  var privProp = 5;
  this.pubProp = 9;
  this.getter = function(x) {
    return privProp + this.pubProp + x;
  };
}
var obj1 = new Constructor();
var obj2 = new Constructor();
var fn1 = obj1.getter;             // Invalid, fn1 will not be bound to obj1.
var fn2 = obj2.getter.bind(obj2);  // Note .bind here.
console.log(fn1(10), fn2(10));

打印类似的输出:

NaN 24

同样,未绑定到fn1的{​​{1}}返回无效输出。