Skip to content

Instantly share code, notes, and snippets.

@terrierscript
Last active June 1, 2016 13:57
Show Gist options
  • Select an option

  • Save terrierscript/044fa094c7c65c89f26c36660cfb1147 to your computer and use it in GitHub Desktop.

Select an option

Save terrierscript/044fa094c7c65c89f26c36660cfb1147 to your computer and use it in GitHub Desktop.
babelでlet a = () => {} が let a = function a(){}と匿名関数に名前が付けられるのはなんでなんでなの?というのを調べた ref: http://qiita.com/inuscript/items/e820652ee0d68dd2c190
let a = () => {} // arrow function
let b = function(){} // 普通のfunction(無名)
let c = function d(){} // 名前付きfunction
% $(npm bin)/babel foo.js --presets es2015
function hogeFunctionGenerator(someFlag, defaultCallback){
if(someFlag){
return function someFlagFn(){}
}
return defaultCallback
}
// どの関数が結局コールされるのか知りたい場合
console.log(hogeFunctionGenerator(true, function (){}).name) // "someFlagFn"
console.log(hogeFunctionGenerator(false, function (){}).name) // ""
console.log(hogeFunctionGenerator(false, function cb(){}).name) // "cb"
class Hoge {}
function createClassZ() {
// Z classを返す
return class Z{
constructor(){
}
}
}
function createAnonymousClass() {
// 匿名classを返す
return class {
constructor(){
}
}
}
let ZX = createClassZ()
let zx = new ZX()
let X = createAnonymousClass()
let x = new X()
console.log("zx", zx.constructor.name)
console.log("x", x.constructor.name)
zx Z
x undefined
zx Z
x _class
"use strict";
var a = function a() {};
var b = function b() {};
var c = function d() {};
% $(npm bin)/babel foo.js --plugins transform-es2015-arrow-functions
let a = function () {}; // まだ無名!
let b = function () {};
let c = function d() {};
% $(npm bin)/babel foo.js --plugins transform-es2015-function-name
let a = () => {};
let b = function b() {}; // 名前がついた!
let c = function d() {};
% $(npm bin)/babel foo.js --plugins transform-es2015-function-name,transform-es2015-arrow-functions
let a = function a() {};
let b = function b() {};
let c = function d() {};
// Function Statements
function fn () {}
console.log(fn.name) // "fn"
// variables
const a = function(){}
console.log(a.name) // "a"
const b = () => {}
console.log(b.name) // "b"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment