Jump to content


Photo

Is there a build in javascript "this" variable?


  • Please log in to reply
2 replies to this topic

#1 markus_ja

markus_ja
  • Members
  • 319 posts
  • LocationAustria

Posted 16 March 2016 - 05:31 PM

Hello,

 

I use jQuery a lot. When passing a function to a jquery function, "this" is used to refere to the current object. Currently, I just use the asm section to access that variable. Is there a more elegant way?

 

e.g.:

jQuery(divMyTable, '#id-amount').each(
    procedure
    var
      this: Variant;
      amount: float;
    begin
      asm @this = this; end; //I would like to avoid using asm section regulary in my project.

      amount := TVariant.AsFloat(jQueryEx(this).text());
      Inc(amount);
      
      jQueryEx(this).text(amount);
    end);

When I write a function, the this variable is used from a different scope. Inlining also doesn't help.

function W3This: variant; inline;

function W3This: variant;
begin
  asm @result = this; end; //Wrong scope
end;


#2 Laksekjønn

Laksekjønn
  • Members
  • 440 posts

Posted 16 March 2016 - 05:51 PM

external class
  var this external "this" : THandle;
 
native class:
type
   TMyObj = class
      function this : TMyObj;
   end;
 
function TMyObj.this : TMyObj;
begin
   Result:=Self;
end;

  • Nico Wouterse likes this

#3 markus_ja

markus_ja
  • Members
  • 319 posts
  • LocationAustria

Posted 16 March 2016 - 05:57 PM

Thanks! Works perfect :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users