markbates / markbates/Programming-In-CoffeeScript

coffeescript and javascript handle return values in constructors differently

Đang mở
#4 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
CoffeeScript
Star
62
Fork
20
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

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);

```

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách so sánh person.coffee, bad_person.coffee và redeemed_bad_person.js, tập trung vào cách các giá trị trả về của constructor ảnh hưởng đến new. Xác nhận hành vi bằng các phiên bản CoffeeScript và Node.js được mô tả; công việc được xem là hoàn tất khi gotcha và mẫu constructor được khuyến nghị đã được ghi lại trong phần nội dung sách liên quan.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
coffeescript, javascript, node.js
Lĩnh vực
documentation
Loại issue
Tài liệu
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.