Skip to content

Instantly share code, notes, and snippets.

@alphaKAI
Created September 13, 2015 16:43
Show Gist options
  • Save alphaKAI/40f3e5cf53469facfd19 to your computer and use it in GitHub Desktop.
Save alphaKAI/40f3e5cf53469facfd19 to your computer and use it in GitHub Desktop.
TypeScriptでインスタンスを共有する
class Test1{
varStr: string;
constructor(){
this.varStr = "ABC";
}
}
class Test2{
private t1: Test1;
constructor(t1: Test1){
this.t1 = t1;
}
setVal(str: string){
this.t1.varStr = str;
}
getVal(): string{
return this.t1.varStr;
}
}
class Test3{
private t1: Test1;
constructor(t1: Test1){
this.t1 = t1;
}
getVal(): string{
return this.t1.varStr;
}
}
var t1 = new Test1();
var t2 = new Test2(t1);
var t3 = new Test3(t1);
console.log("FIRST - " + t1.varStr);
t2.setVal("XXX");
console.log("SECOND(t1) - " + t1.varStr);
console.log("SECOND(t2) - " + t2.getVal());
console.log("SECOND(t3) - " + t3.getVal());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment