문제

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

  }
}
도움이 되었습니까?

해결책

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
},
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top