Question

top.on('click', function(){
    anim.run();
});

J'ai une fonction d'animation et je me demandais pourquoi je ne pouvais pas l'appeler ainsi

top.on('click', anim.run);
Était-ce utile?

La solution

top.on('click', function () { anim.run(); });

ou

top.on('click', Y.bind(anim.run, anim));

Autres conseils

Parce que , ce n'est pas anim dans le deuxième cas, car vous récupérez la fonction run et ne l'appelez pas à partir de anim .

Par exemple:

var a = {
  b: function () {
    return this.c;
  },
  c: 1
},
c = 2;

a.b() === 1;
var bMethod = a.b;
bMethod() === 2;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top