Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript Class how to define not prototype class function

Tags:

typescript

Just starting to use TypeScript and I can't find explanation for this issue...

Lets say I have function

function test() {
    function localAccessMethod() {
        console.log('I am only accessable from inside the function :)');
    }

    this.exposedMethod = function () {
        console.log('I can access local method :P');

        localAccessMethod();
    }
}

And I want to convert this to typescript class... So far I did it up to here:

class test {

    constructor: {}

    exposedMethod() {
        console.log('I can access local method :P');

        localAccessMethod();
    }

}

How can I define that local function in the typescript class, so it wouldn't be exposed as prototype or .this... ?

Or better question, how should I convert the source code to fit TypeScript standard. I want to have function which is available for all class methods only, but is not exposed...

like image 327
Tautvydas Avatar asked Sep 01 '25 20:09

Tautvydas


1 Answers

You can to use private static keywords:

class Test
{
  private static localAccessMethod()
  {
    console.log('I am only accessable from inside the function :)');
  }

  exposedMethod()
  {
    console.log('I can access local method :P');
    Test.localAccessMethod();
  }
}
like image 182
ktretyak Avatar answered Sep 03 '25 20:09

ktretyak