Lodash _.neg() 方法
Lodash是一个基于 underscore.js 的 JavaScript 库。 Lodash 有助于处理数组、字符串、对象、数字等。
_.neg() 方法用于返回一个与给定数字相反符号值的新数字。
句法:
_.neg( value )
参数:此方法接受如上所述和如下所述的单个参数:
- value:该方法要取反的数字。
返回值:此方法返回一个与给定数字符号值相反的新数字。
注意:这在普通 JavaScript 中不起作用,因为它需要安装 lodash contrib 库。 Lodash contrib 库可以使用npm install lodash-contrib –save 安装。
示例 1:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Using the _.neg() method
var n = _.neg( -1 );
console.log(
"Opposite sign value number is :", n
);
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Using the _.neg() method
var n = _.neg( 1 );
console.log(
"Opposite sign value number is :", n
);
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Using the _.neg() method
var n = _.neg( 60 );
console.log(
"Opposite sign value number is :", n
);
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Using the _.neg() method
var n = _.neg( -50 );
console.log(
"Opposite sign value number is :", n
);
输出:
Opposite sign value number is : 1
示例 2:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Using the _.neg() method
var n = _.neg( 1 );
console.log(
"Opposite sign value number is :", n
);
输出:
Opposite sign value number is : -1
示例 3:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Using the _.neg() method
var n = _.neg( 60 );
console.log(
"Opposite sign value number is :", n
);
输出:
Opposite sign value number is : -60
示例 4:
Javascript
// Defining lodash contrib variable
var _ = require('lodash-contrib');
// Using the _.neg() method
var n = _.neg( -50 );
console.log(
"Opposite sign value number is :", n
);
输出:
Opposite sign value number is : 50