markbates / markbates/Programming-In-CoffeeScript

coffeescript and javascript handle return values in constructors differently

Abierto
#4 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
CoffeeScript
Estrellas
62
Forks
20
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

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




Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza comparando person.coffee, bad_person.coffee y redeemed_bad_person.js, centrándote en cómo los valores de retorno de los constructores afectan a new. Confirma el comportamiento con las versiones de CoffeeScript y Node.js descritas; se considera terminado cuando el problema y el patrón de constructor recomendado estén documentados en el material relevante del libro.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
coffeescript, javascript, node.js
Área
documentation
Tipo de issue
Documentación
Dificultad
2/5
Tiempo estimado
1-3 horas
Estado de actividad
Estancado
Claridad
Necesita aclaración
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.