W
W
Wynell_ru2019-06-13 12:01:25
CoffeeScript
Wynell_ru, 2019-06-13 12:01:25

How to execute object method via do in CoffeeScript?

There is an object, for example, with two methods that return the same object.
The first one accepts a parameter, the second one does not.
There is no problem with calling the first one.

obj
.firstMethod "firstParameter"

But here's how to add the same line like `.method [params]` using regular CoffeeScript? (using `do`)
If I write
obj
._1 "firstParameter"
.do secondMethod

then compiles to
obj.firstMethod("firstParameter").do(secondMethod);

Naturally, not what was expected,
but something like: Is it possible to somehow implement this using exactly `do`? Yes, of course, you can put `do` at the very beginning, but it's not a fact that a method without parameters is the last one in the list.
obj.firstMethod("firstParameter").secondMethod();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Taratin, 2019-06-15
@Wynell_ru

class Klass
  methodA: -> 
    console.log arguments
    this   

  methodB: ->
    console.log arguments
    this
   

obj = new Klass

(do (obj.methodA "aaa").methodB).methodA "aaa"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question