markbates / markbates/Programming-In-CoffeeScript

coffeescript and javascript handle return values in constructors differently

Ouverte
#4 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
CoffeeScript
Étoiles
62
Forks
20
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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




Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par comparer person.coffee, bad_person.coffee et redeemed_bad_person.js, en vous concentrant sur la manière dont les valeurs de retour des constructeurs affectent new. Confirmez le comportement avec les versions de CoffeeScript et Node.js décrites ; le travail est terminé lorsque le piège et le modèle de constructeur recommandé sont documentés dans le contenu pertinent du livre.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
coffeescript, javascript, node.js
Domaine
documentation
Type d'issue
Documentation
Difficulté
2/5
Temps estimé
1-3 heures
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.