coffeescript and javascript handle return values in constructors differently
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 35/100
- Issue-Typ
- Dokumentation
- Klarheit
- Muss geklärt werden
- Aktivitätsstatus
- Veraltet
- Tech-Stack
- coffeescript, javascript, node.js
- Bereich
- documentation
Rechercherichtung
Beginne mit dem Vergleich von person.coffee, bad_person.coffee und redeemed_bad_person.js und konzentriere dich darauf, wie Rückgabewerte von Konstruktoren new beeinflussen. Bestätige das Verhalten mit den beschriebenen CoffeeScript- und Node.js-Versionen; abgeschlossen ist die Aufgabe, wenn der Stolperstein und das empfohlene Konstruktormuster im relevanten Buchmaterial dokumentiert sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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);
- Vorherrschende Sprache
- CoffeeScript
- Sterne
- 62
- Forks
- 20
- PR-Merge-Kennzahlen
- Keine gemergten PRs in 30 T.
Beitragsleitfaden
Für dieses Repository ist kein Beitragsleitfaden indexiert
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus markbates/Programming-In-CoffeeScript
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 45/100
markbates/Programming-In-CoffeeScript#3 · 1 Kommentar ·
-
chpt 10 Offen
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 25/100
markbates/Programming-In-CoffeeScript#2 · 2 Kommentare ·
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 25/100
markbates/Programming-In-CoffeeScript#1 · 7 Kommentare ·
Alle Issues in markbates/Programming-In-CoffeeScript
Ähnliche Issues
-
content
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
GDQuest/learn-gdscript#1395 ·
-
rules
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
-
HttpClient
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
symfony/symfony-docs#23092 ·
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 90/100
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 95/100
huggingface/sentence-transformers#4074 ·