markbates / markbates/Programming-In-CoffeeScript
coffeescript and javascript handle return values in constructors differently
まだ誰も着手していません。
- 主要言語
- CoffeeScript
- スター
- 62
- フォーク
- 20
- PR マージ指標
- 30日以内にマージされた PR はありません
説明
I was once under the false impression that it didn't matter what value a constructor returned with used with new as in p = new Person()
But, I've told by the gurus at the node.js and coffee-script github sites that this is not so.
In general, with coffeescript it's best to use class as in: class Person ...
But, if one tries to create a function that acts as a constructor in CoffeeScript, there is at least one gotcha. The follow three short scripts demonstrate the problem, which apparently the writers of CoffeeScript and JavaScript will simply have to deal with. (The gurus who create/maintain these languages don't think its their problem and closed my bug report immediately.)
So, Mark, as someone who writes books on CoffeeScript, I hope the following might help you warn your readers. (BTW: like your book)
#-------------------------- person.coffee -----------------------------------
Person = (name) ->
@name = name
@greet_friend = (friend) ->
console.log("Hello #{friend}. My name is #{@name}.")
###
wierd CoffeeScript/Node.js error:
You will get and error if you comment the next line out
and explicitly return the greet_friend method above.
###
return null
Person.prototype.hello_world = ->
console.log("#{@name} says hello world.")
p = new Person('Fred')
p.greet_friend('Mary')
p.hello_world()
#------------------------- bad_person.coffee ---------------------
Person = (name) ->
@name = name
@greet_friend = (friend) ->
console.log("Hello #{friend}. My name is #{@name}.")
###
wierd CoffeeScript v1.6.1/Node.js v0.8.22 error:
This will bomb because we've commented out
the 'return null' line below, which means
that CoffeeScript will explicitly return
the greet_friend method above.
return null
###
Person.prototype.hello_world = ->
console.log("#{@name} says hello world.")
p = new Person('Fred')
p.greet_friend('Mary')
p.hello_world()
#------------------- redeemed_bad_person.js -----------------------
// Generated by CoffeeScript 1.6.1
// but modified by me. I removed the
// explicit return of the greet_friend
// method.
(function() {
var Person, p;
Person = function(name) {
this.name = name;
this.greet_friend = function(friend) {
return console.log("Hello " + friend + ". My name is " + this.name + ".");
};
/*
wierd CoffeeScript v1.6.1/Node.js v0.8.22 error:
This version works because we do not
explicitly return the greet_friend
method above.
*/
};
Person.prototype.hello_world = function() {
return console.log("" + this.name + " says hello world.");
};
p = new Person('Fred');
p.greet_friend('Mary');
p.hello_world();
}).call(this);
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず person.coffee、bad_person.coffee、redeemed_bad_person.js を比較し、コンストラクターの戻り値が new にどのような影響を与えるかに注目します。記載されている CoffeeScript と Node.js のバージョンで動作を確認します。関連する書籍の内容に、この落とし穴と推奨されるコンストラクターパターンを記載できれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- coffeescript, javascript, node.js
- 領域
- documentation
- issue の種類
- ドキュメント
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 活発さ
- 停滞
- 明瞭さ
- 説明が足りない
- 初心者へのやさしさ
- 35/100