markbates / markbates/Programming-In-CoffeeScript
coffeescript and javascript handle return values in constructors differently
还没有人认领这个 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);
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 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