Check if function parameter is undefined in SmartMS?

How to check if function parameter is undefined?

procedure Test(aValue: TObject);
begin
  if aValue <> nil then
    ShowMessage('param filled')      <-- also when Test() is executed!
  else
    ShowMessage('no param filled')   <-- not called, only when Test(nil) is called
end;

However, when this function is called in pure JS without a parameter, then aValue = undefined, but the check <> nil is converted to == null!

For example, when you have a JS function with a callback:

type
  TObjectProcedure = procedure(aValue: TObject);

procedure FetchUrlAsync(aUrl: string; aCallback: TObjectProcedure )
begin
  asm
    $().load(@aUrl, @aCallback);
  end;
end;

You can call this function with:

FetchUrlAsync('ajax/test.html', Test);

Now it depends on jQuery if "Test" is called with a parameter or not.

+3
source share
2 answers

In the next version, you will be able to use the Defined () special function, it will do a strict check against undefined (it will return true for a null value).

if Defined(aValue) then
   ...

In the current version, you can define a function to verify that

function IsDefined(aValue : TObject);
begin
   asm
      @result = (@aValue!==undefined);
   end;
end;
+4
source

In the current version (1.0), you can use the varIsValidRef () function to check if the value is undefined. The function is part of w3system.pas, so it is always present. It looks like this:

function varIsValidRef(const aRef:Variant):Boolean;
begin
  asm
    if (@aRef == null) return false;
    if (@aRef == undefined) return false;
    return true;
  end;
end;

This checks both null and undefined, so you can use it to refer to objects (the THandle type is also an option).

0
source

All Articles