ClojureScript

Create a standard way to reference this

Details

  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Critical Critical
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None

Description

Some js frameworks require you to implement 'methods' and those methods need to access js this. Currelty this forces you to js*.

_the following is handled by CLJS-83_

Currently deftype only supports implementing protocol functions. It could be enhanced to support 'method' fns, possibly by using the Object section:

(deftype Foo [a]
Object
(anyArbitraryMethod [a-name-for-this] ...))

Note that the arity of the actual fn would be one less than for protocol 'methods'.

Explore other options before going this way.

Supporting access to 'this' in stand-alone functions may cause gclosure to complain (it only wants to see traditional methods on prototypes), and is a non-objective for now.

Activity

Hide
Fogus added a comment -

The plan is to start with a pair, this-as and extend-object!. used as such:

;;;;;; GLOBAL THIS

(defn f [] 
  (this-as self 
    (println self)) 
  [self])

(f)
;; #<Object Global>
;;=> [#<undefined>] 

;;;;;; METHOD THIS

(def o (array))

(extend-object! o {:foo f})

(. o (foo))
;; #<Array []>
;;=> [#<undefined>]
Show
Fogus added a comment - The plan is to start with a pair, this-as and extend-object!. used as such:
;;;;;; GLOBAL THIS

(defn f [] 
  (this-as self 
    (println self)) 
  [self])

(f)
;; #<Object Global>
;;=> [#<undefined>] 

;;;;;; METHOD THIS

(def o (array))

(extend-object! o {:foo f})

(. o (foo))
;; #<Array []>
;;=> [#<undefined>]
Hide
Fogus added a comment - - edited

I am going to split out the (deftype T ... Object ...) capability into its own ticket as it's scope is greater than the accessing of this. It's likely that the new ticket will eliminate the need for extend-object!.

Show
Fogus added a comment - - edited I am going to split out the (deftype T ... Object ...) capability into its own ticket as it's scope is greater than the accessing of this. It's likely that the new ticket will eliminate the need for extend-object!.
Hide
Fogus added a comment -

Added note about related ticket. (CLJS-83)

Show
Fogus added a comment - Added note about related ticket. (CLJS-83)
Hide
Fogus added a comment -

The this-as macro provides a scoped way to access the implicit this.

Show
Fogus added a comment - The this-as macro provides a scoped way to access the implicit this.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: