Skip to main content

Class Pointers in CoffeeScript?



trying to figure out how to write the following in CoffeeScript:







var foo = new function()

{



var $this = this;



$("#foo").click( this.clicked );



this.clicked = function()

{

$this.alert( $(this).text() );

};



this.alert = function(message)

{

alert(message);

};



};







Unfortunately I can't figure out for the life of me how in CoffeeScript I access the class pointer, "this" is obviously not context aware and will often just point to the variable passed by the callee. So there's no way for me to write the above script in CoffeeScript.





Any advice? I can't find anything useful in the documentation, you have the @ pointers but they also just use the "this" pointer from the current context, making it useless..


Comments

  1. You can add methods directly to @ in the constructor to achieve the same effect:

    class C
    constructor: ->
    $this = @
    @clicked = ->
    console.log @
    $this.alert 'pancakes'
    alert: (m) ->
    console.log m

    c = new C
    c.clicked()
    c.clicked.call window​​​​​


    Demo: http://jsfiddle.net/ambiguous/Y8ZBe/

    You'd usually be able to use a bound method and an "event" argument in situations like this though:

    class C
    clicked: (ev) =>
    @alert(ev.target.value)
    alert: (m) ->
    console.log m

    c = new C
    c.clicked(target: { value: 'pancakes' })
    c.clicked.call window, target: { value: 'pancakes' }


    This sort of thing usually turns up in jQuery (or similar) callbacks and they usually have an event argument which explicitly identifies the target "this" so that you can use bound functions.

    Demo: http://jsfiddle.net/ambiguous/LafV2/

    ReplyDelete
  2. CoffeeScript is still javascript. The limitations of this still apply. You can obviously write a straight translation:

    foo = ->
    self = this
    @clicked = ->
    self.alert $(this).text()
    @alert = (message) ->
    alert message
    $('#foo').click @clicked


    Yet you should be using prototypes (even in javascript). With the fat arrow => you can bind the function to it's current context (but then you lose the element reference):

    foo = ->
    $('#foo').click (e) =>
    @clicked(e.target)

    foo::clicked = (el) ->
    @alert $(el).text()

    foo::alert = (message) ->
    alert message


    Or using the class abstraction (nothing more than prototype usage wrapped up in a prettier package) and jQuery.proxy:

    class Foo
    constructor: ->
    $('#foo').click $.proxy @clicked, this
    clicked: (e) ->
    @alert $(e.target).text()
    alert: (message) ->
    alert message


    $.proxy @clicked, this could be replaced with @clicked.bind @ on modern browsers/engines (see Function.prototype.bind).

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.