Last active
October 17, 2015 23:25
-
-
Save Deraen/8fede09ce77859004537 to your computer and use it in GitHub Desktop.
Extending Closure Classes From Clojurescript. Original source: http://www.50ply.com/blog/2012/07/08/extending-closure-from-clojurescript/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns frontend.editor-plugin | |
(:require-macros [frontend.goog-extend :refer [goog-extend]]) | |
(:import [goog.editor Plugin])) | |
; 4. Now the code should work with advanced compilation but Closure Compiler | |
; will display few warnings about using "this" in static functions. | |
; To fix this, the constuctor should have proper JSDoc comments: | |
(goog-extend ^{:jsdoc ["@constructor" "@extends {goog.editor.Plugin}"]} FooPlugin Plugin | |
([] | |
; 1. This doesn't work with advanced compilation as Closure compiler checks that first | |
; parameter to goog/base is named "this" and Clojurescript compiler will name it "this$" | |
; (this-as this (goog/base this)) | |
; 2. This doesn't seem to work for advanced compilation, "this" gets renamed? | |
; (goog/base (js* "this")) | |
; 3. Instead of calling goog/base, it's possible to call base class constructor | |
; directly (Closure Library code is doing this) | |
(this-as this (.call Plugin this)) | |
) | |
...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns frontend.goog-extend) | |
(defn- to-property [sym] | |
(symbol (str "-" sym))) | |
(defmacro goog-extend [type base-type ctor & methods] | |
`(do | |
(def ~type (fn ~@ctor)) | |
(goog/inherits ~type ~base-type) | |
~@(map | |
(fn [method] | |
`(set! (.. ~type -prototype ~(to-property (first method))) | |
(fn ~@(rest method)))) | |
methods))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment