Skip to content

Instantly share code, notes, and snippets.

@kenmori
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenmori/fb188add864fd5a67573 to your computer and use it in GitHub Desktop.
Save kenmori/fb188add864fd5a67573 to your computer and use it in GitHub Desktop.
TypeScriptでConstructor外で関数を宣言型か無名関数を代入するかでの実行比較 ref: http://qiita.com/M-ISO/items/4401c2ff1c34f4f87156
module A{
interface ImoritaA{
name:string;
}
class Aclass implements ImoritaA{
constructor(public name){
this.name = "fafa"
}
public func1= ()=>{//constructor外、無名関数を代入
}
public func2(){ //constructor外、宣言型
}
}
}
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 = {}));
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())
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());
//同じ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
//略
constructor(
){
//const実行内
}
f = ()=>{}
this.f() = function(){}
class MoritaB{
constructor(){
}
name = 'fafa';
}
var MoritaB = (function () {
function MoritaB() {
this.name = 'fafa';
}
return MoritaB;
})();
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