Last active
June 1, 2016 13:57
-
-
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
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
| let a = () => {} // arrow function | |
| let b = function(){} // 普通のfunction(無名) | |
| let c = function d(){} // 名前付き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
| % $(npm bin)/babel foo.js --presets es2015 |
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
| 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" |
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 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) |
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
| zx Z | |
| x undefined |
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
| zx Z | |
| x _class |
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
| "use strict"; | |
| var a = function a() {}; | |
| var b = function b() {}; | |
| var c = function d() {}; |
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
| % $(npm bin)/babel foo.js --plugins transform-es2015-arrow-functions |
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
| let a = function () {}; // まだ無名! | |
| let b = function () {}; | |
| let c = function d() {}; |
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
| % $(npm bin)/babel foo.js --plugins transform-es2015-function-name |
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
| let a = () => {}; | |
| let b = function b() {}; // 名前がついた! | |
| let c = function d() {}; |
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
| % $(npm bin)/babel foo.js --plugins transform-es2015-function-name,transform-es2015-arrow-functions |
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
| let a = function a() {}; | |
| let b = function b() {}; | |
| let c = function d() {}; |
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
| // 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