I have a class in my application
class MyClass {
doTask1 = () => {
doTask2();
}
doTask2 = () => {
}
}
How can I set up an interface file and how do I decorate my methods for this so that doTask1 is public and doTask2 is private?
How can I set up an interface file
Javascript does not strictly have interfaces, see this stack overflow question
how do I decorate my methods for this so that doTask1 is public and doTask2 is private?
In Javascript, the idea of public and private is sort of not there, although many people follow the convention of appending and underscore to the variable or function name (such as _foo
) to let others know it was meant to be private. However, you can control access to variables, here is a good explanation of that written by Douglas Crockford.
EDIT
For TypeScript specifically, I'd checkout out the MSDN article on interfaces , and I think that this stack overflow question is relevant to your question about private functions.
Remember - Interfaces can only contain public methods:
interface IMyClass {
doTask1(): void;
}
class MyClass implements IMyClass {
doTask1 = () => {
this.doTask2();
}
doTask2 = () => {
}
}
var instance: IMyClass = new MyClass();
instance.doTask1(); // Ok
instance.doTask2(); // Error
Runnable Example: http://bit.ly/Uzkv7V
Btw, You should probably structure your class like this:
class MyClass implements IMyClass {
doTask1() {
this.doTask2();
}
private doTask2() {
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.