Coffeescript-math-abs

提供:Dev Guides
移動先:案内検索

CoffeeScript Math-abs()メソッド

説明

このメソッドは整数を受け入れ、指定された整数の絶対値を返します。

構文

このメソッドの構文は次のとおりです。

Math.abs( x )

次の例は、CoffeeScriptでの* abs()メソッドの使用方法を示しています。 このコードを *math_abs.coffee という名前のファイルに保存します。

value = Math.abs(-1);
console.log "The absolute value of -1 is : " + value

value = Math.abs(null);
console.log "The absolute value of null is : " + value

value = Math.abs(20);
console.log "The absolute value of 20 is : " + value
  • コマンドプロンプト*を開き、以下に示すように.coffeeファイルをコンパイルします。
c:\> coffee -c math_abs.coffee

コンパイル時に、次のJavaScriptが提供されます。

//Generated by CoffeeScript 1.10.0
(function() {
  var value;

  value = Math.abs(-1);

  console.log("The absolute value of -1 is : " + value);

  value = Math.abs(null);

  console.log("The absolute value of null is : " + value);

  value = Math.abs(20);

  console.log("The absolute value of 20 is : " + value);

}).call(this);

次に、*コマンドプロンプト*を再度開き、以下に示すようにCoffeeScriptファイルを実行します。

c:\> coffee math_abs.coffee

CoffeeScriptファイルを実行すると、次の出力が生成されます。

The absolute value of -1 is : 1
The absolute value of null is : 0
The absolute value of 20 is : 20