目录

abs()

描述 (Description)

此方法接受整数并返回给定整数的绝对值。

语法 (Syntax)

以下是此方法的语法。

Math.abs( x )

例子 (Example)

以下示例演示了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

打开command prompt并编译.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); 

现在,再次打开command prompt ,然后运行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
↑回到顶部↑
WIKI教程 @2018