1

私は Javascript を学んでいるので、この質問はほとんどの JS コーダーにとってばかげているように思えるかもしれません。私はJavascriptを読んでいます:良い部分ですが、このコードを機能させることはできません:

Function.prototype.method = function(name,func){
    this.prototype[name] = func;
    return this;
}
Number.method('integer', function(){
    return Math[ this <0? 'ceiling' : 'floor'](this);
});
document.writeln(Math.floor(3.4)+"");
document.writeln((-10/3).integer());

おそらくご想像のとおり、最初の document.writeln 関数は本来あるべき "3" を表示しますが、2 番目の関数は何も表示せず、エラーは "TypeError: Math["floor"] is not a function" です。機能。

これはばかげていると確信していますが、なぜ機能しないのかわかりません。御時間ありがとうございます。

ファビアン

4

2 に答える 2

3

「ceiling」を「ceil」にすると、うまく動作します。テストしました:

Number.method('integer', function(){
    return Math[ this <0? 'ceil' : 'floor'](this);
});
于 2013-09-19T09:48:15.757 に答える
1

「床」は確かに数学関数です。しかし、あなたのコードは「ceil」であるはずの「ceiling」を返します

于 2013-09-19T09:42:26.783 に答える