markbates / markbates/Programming-In-CoffeeScript

coffeescript and javascript handle return values in constructors differently

未關閉
#4 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

主要語言
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);

```

貢獻指南

這個儲存庫沒有索引到貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 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

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。