Question

I'm a JavaScript developer. I use YUI3 for client / serveur development. I want to try coffeescript but I have a problem. In order to do POO with YUI, I must use this structure :

MyClass = function(){
    MyClass.superclass.constructor.apply(this, arguments);
};

MyClass.prototype = {
    initializer: function (arguments) {

    },
    otherFunction: (){}
}

http://yuilibrary.com/yui/docs/base/

I can't rename arguments so the compilator coffescript send me :

error: parameter name "arguments" is not allowed                                                                                                                                                                            initializer: (arguments) ->

Edit :

Without "arguments"

MyClass = function(args) {
  return MyClass.superclass.constructor.apply(this, args);
};
MyClass.prototype = {
  initializer: function(args) {

  }
}
Était-ce utile?

La solution

Having "arguments" as an argument-name in Javascript is a bad idea, if it were even possible. (god i hope not) "arguments" Is a "keyword-like-thingy" that always "returns/contains/represents" an array-like object containing all arguments given in the function it encloses.

Example:

function foo() {
  console.log(arguments);
}

foo("a", "b"); // prints something like {0: "a", 1: "b"}

Furthermore, the apply() method receives an array (or array-like object) of arguments to call the function with.

function bar(a, b) {
  console.log([a, b]);
}

bar.apply(this, [1, 2]);  // prints something like [1, 2]

Therefore, you can pass the arguments-object to .apply to invoke a method with the same arguments the enclosing function is called.

In other words, you might just want to omit the "arguments" argument in your initializer. It is already there when your initializer is invoked.

initializer: function () {
  console.log(arguments); // no error, i'm here for you
},
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top