4

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...

2 Answers 2

2

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();
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can't have something in a class which won't be exposed, even private/protected members/methods are exposed in javascript and only the compiler enforces this visibility.

You have a few options:

(1) Have the "inner" function inside the main one:

export class test {
    constructor() {}

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

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

        localAccessMethod();
    }
}

(2) If this is a module then place the inner function in the top level part of the module and don't export it:

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

export class test {
    constructor() {}

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

        localAccessMethod();
    }
}

(3) If you're not using modules then wrap this thing in a namespace and export only the class:

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

    export class test {
        constructor() {}

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

            localAccessMethod();
        }
    }
}

1 Comment

Thank you for detailed answer. Looks like third option fits my needs best in this case, as I want to have multiple private methods which will share not exposed function and I'm not yet using modules... Thank you Nitzan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.