[Pas2js] Question regarding the future type helper feature
warleyalex
warleyalex at yahoo.com.br
Sun Jan 6 14:11:40 CET 2019
silvioprog wrote
> (...) I suspect you will implement it extending the object prototype (...)
DWScript supports "multiple class helpers within scope". That is, MORE THAN
ONE class helper for a given class may be in scope at any time. For
instance, if you have two helpers in scope, ALL OF THEM will be recognised
by the compiler. I suspect they have implemented helpers "without extending
the object prototype".
--------
type
TPessoa = class
private
FDataNasc: TDateTime;
public
property DataNasc: TDateTime read FDataNasc write FDataNasc;
end;
TPessoaHelper = class helper for TPessoa
private
function GetIdade: integer;
public
property Idade: integer read GetIdade;
end;
//TPessoaFisicaHelper = class helper (TPessoaHelper) for TPessoa
TPessoaFisicaHelper = class helper for TPessoa
public
function isValidCPF():Boolean;
end;
//TPessoaJuridicaHelper = class helper (TPessoaFisicaHelper) for TPessoa
TPessoaJuridicaHelper = class helper for TPessoa
public
function isValidCNPJ():Boolean;
end;
TIntHelper = record helper for integer
public
function ToStr(): String;
end;
implementation
function TPessoaFisicaHelper.isValidCPF(): Boolean;
begin
console.log('CPF is valid');
end;
function TPessoaJuridicaHelper.isValidCNPJ(): Boolean;
begin
console.log('CNPJ is valid');
end;
{ TPessoaHelper }
function TPessoaHelper.GetIdade: integer;
begin
Result := Round((Now - Self.DataNasc) / 365.25);
end;
{ TIntHelper }
function TIntHelper.ToStr: String;
begin
Result := IntToStr(Self);
end;
--------
Example:
======
var
oPessoa: TPessoa;
begin
oPessoa := TPessoa.Create;
oPessoa.DataNasc := EncodeDate(1986, 11, 26);
console.log(oPessoa.Idade.ToStr);
oPessoa.isValidCPF;
oPessoa.isValidCNPJ;
end;
=================================
emitted JS
=======
function TPessoaJuridicaHelper$isValidCNPJ(Self) {
console.log("CNPJ is valid");
}
function TPessoaHelper$GetIdade(Self) {
return Math.round((Now()-Self.FDataNasc)/365.25);
}
function TPessoaFisicaHelper$isValidCPF(Self) {
console.log("CPF is valid");
}
var TPessoa = {
$ClassName:"TPessoa",$Parent:TObject
,$Init:function ($) {
TObject.$Init($);
$.FDataNasc = 0;
}
,Destroy:TObject.Destroy
};
function TIntHelper$ToStr(Self) {
return Self.toString();
}
oPessoa = TObject.Create($New(TPessoa));
oPessoa.FDataNasc = EncodeDate$1(1986,11,26);
console.log(TIntHelper$ToStr(TPessoaHelper$GetIdade(oPessoa)));
TPessoaFisicaHelper$isValidCPF(oPessoa);
TPessoaJuridicaHelper$isValidCNPJ(oPessoa);
=================================
--
Sent from: http://pas2js.38893.n8.nabble.com/
More information about the Pas2js
mailing list