Last active
August 29, 2015 14:27
-
-
Save kenmori/fb188add864fd5a67573 to your computer and use it in GitHub Desktop.
TypeScriptでConstructor外で関数を宣言型か無名関数を代入するかでの実行比較 ref: http://qiita.com/M-ISO/items/4401c2ff1c34f4f87156
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module A{ | |
interface ImoritaA{ | |
name:string; | |
} | |
class Aclass implements ImoritaA{ | |
constructor(public name){ | |
this.name = "fafa" | |
} | |
public func1= ()=>{//constructor外、無名関数を代入 | |
} | |
public func2(){ //constructor外、宣言型 | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var A; | |
(function (A) { | |
var Aclass = (function () { | |
function Aclass(name) { | |
this.name = name; | |
this.func1 = function () {//無名関数を代入するとconstructor実行内に記述されたことになる | |
}; | |
this.name = "fafa"; | |
} | |
Aclass.prototype.func2 = function () {//prototypeメソッドとして設定される | |
}; | |
return Aclass; | |
})(); | |
})(A || (A = {})); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module A{ | |
export interface ImoritaA{ | |
name:string; | |
} | |
export class Aclass implements ImoritaA{ | |
name:string; | |
func1: Function;//追加 | |
constructor(){ | |
this.name = "fafa" | |
this.func1 = ()=>{//constructor内に移動 | |
alert('func1です') | |
} | |
} | |
public func2(){ | |
alert('func2です'); | |
} | |
} | |
} | |
//追加記述 実行箇所 | |
var fafa = new A.Aclass(); | |
console.log(fafa.func1()) | |
console.log(fafa.func2()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var A; | |
(function (A) { | |
var Aclass = (function () { | |
function Aclass() { | |
this.name = "fafa"; | |
this.func1 = function () { | |
alert('func1です'); | |
}; | |
} | |
Aclass.prototype.func2 = function () { | |
alert('func2です'); | |
}; | |
return Aclass; | |
})(); | |
A.Aclass = Aclass; | |
})(A || (A = {})); | |
var fafa = new A.Aclass(); | |
console.log(fafa.func1()); | |
console.log(fafa.func2()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//同じmodule内の別classの関数を呼び出す | |
module A{ | |
export interface ImoritaA{ | |
name:string; | |
} | |
export class Aclass implements ImoritaA{ | |
name:string; | |
func1: Function; | |
fromExternalFunc:Function;//追加 | |
constructor(fromExternalFunc){ | |
this.name = "fafa" | |
this.fromExternalFunc = fromExternalFunc | |
} | |
public func2(){ | |
this.fromExternalFunc()//実行 | |
} | |
} | |
export class Bclass{ | |
static b(){ | |
alert('eee'); | |
} | |
} | |
} | |
//変更 実行箇所 | |
var bfafa = A.Bclass.b;//staticだから | |
var fafa = new A.Aclass(bfafa); | |
console.log(fafa.func2())//eee |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//略 | |
constructor( | |
){ | |
//const実行内 | |
} | |
f = ()=>{} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
this.f() = function(){} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MoritaB{ | |
constructor(){ | |
} | |
name = 'fafa'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MoritaB = (function () { | |
function MoritaB() { | |
this.name = 'fafa'; | |
} | |
return MoritaB; | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MoritaB{ | |
constructor(){ | |
} | |
static name = 'fafa'; | |
} | |
//var MoritaB = (function () { | |
// function MoritaB() { | |
// } | |
// MoritaB.name = 'fafa'; | |
// return MoritaB; | |
//})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment