Comments (2)
import 加module.exports
准备测试文件
./lib/RMath.js
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
class RMath {
constructor() {
this.author = 'melon';
}
inc(x) {
return Number(x) + 1;
}
dec(x) {
return Number(x) - 1;
}
add(a, b) {
return Number(a) + Number(b);
}
subtract(a, b) {
return Number(a) - Number(b);
}
multiply(a, b) {
return Number(a) * Number(b);
}
divide(a, b) {
return Number(a) / Number(b);
}
negate(x){
return -1 * Number(x);
}
modulo(a, b) {
return Number(a) % Number(b);
}
mathMod(a, b) {
const defaultRes = NaN;
const tempA = Number(a);
const tempB = Number(b);
if (tempA || tempB) {
return defaultRes;
}
if (!Number.isSafeInteger(tempA) || !Number.isSafeInteger(tempB)) {
return defaultRes;
}
if (tempB < 0) {
return defaultRes;
}
return ((tempA % tempB) + tempB) % tempB;
}
}
module.exports = {RMath};
test.js
(function (log) {
// const RMath = require("./lib/RMath");
import {RMath} from './lib/RMath.js';
log(RMath);
const beginNum = 5;
const endNum = 9.5;
const defaultNum = 17;
log(RMath.mathMod(beginNum,endNum)); //=> NaN 因为被除数不是整数,所以返回 NaN
log(RMath.modulo(RMath.negate(defaultNum),beginNum)); //=> -2 除数可以是负数。
log(RMath.mathMod(RMath.negate(defaultNum),beginNum)); //=> 3 除数可以是负数,但是mathMod方法返回值,只会是正数。
const mathModSelf = x => RMath.mathMod(defaultNum,x);// 类似于 defaultNum % x
const selfMathMod = x => RMath.mathMod(x,defaultNum);// 类似于 x % defaultNum
log([1, 2, 3].map(mathModSelf)); //=> [ 0, 1, 2 ]
log([1, 2, 3].map(selfMathMod)); //=> [ 1, 2, 3 ]
})(console.log)
执行命令
卒
$ node test.js
import {RMath} from './lib/RMath.js';
^
SyntaxError: Unexpected token {
at new Script (vm.js:79:7)
at createScript (vm.js:251:10)
at Object.runInThisContext (vm.js:303:10)
at Module._compile (internal/modules/cjs/loader.js:656:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
at startup (internal/bootstrap/node.js:285:19)
from learn-es6.
回到require
加 exports
还说不用node
的require
,日常捂脸.jpg,真的是真香现场
准备测试文件
./lib/RMath.js
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
class RMath {
constructor() {
this.author = 'watermelon';
}
log(...msg) {
console.log(msg);
}
inc(x) {
// this.log(x);
return Number(x) + 1;
}
dec(x) {
// this.log(x);
return Number(x) - 1;
}
add(a, b) {
// this.log(a, b);
return Number(a) + Number(b);
}
subtract(a, b) {
// this.log(a, b);
return Number(a) - Number(b);
}
multiply(a, b) {
// this.log(a, b);
return Number(a) * Number(b);
}
divide(a, b) {
// this.log(a, b);
return Number(a) / Number(b);
}
negate(x) {
// this.log(x);
return -1 * Number(x);
}
modulo(a, b) {
// this.log(a, b);
return Number(a) % Number(b);
}
mathMod(a, b) {
// this.log(a, b);
const defaultRes = NaN;
const tempA = Number(a);
const tempB = Number(b);
if (!tempA || !tempB) {
return defaultRes;
}
if (!Number.isSafeInteger(tempA) || !Number.isSafeInteger(tempB)) {
return defaultRes;
}
if (tempB < 0) {
return defaultRes;
}
return ((tempA % tempB) + tempB) % tempB;
}
}
module.exports = {
RMath
};
test.js
(function (log) {
const {
RMath
} = require("./lib/RMath");
const rMath = new RMath();
log(RMath); // [Function: RMath]
log(rMath); // RMath { author: 'watermelon' }
const beginNum = 5;
const endNum = 9.5;
const defaultNum = 17;
const negateNum = rMath.negate(defaultNum);
const arrNum = [1, 2, 3];
let objNum = new Map();
objNum.set('x', 1);
objNum.set('y', 2);
objNum.set("z", 3);
log(rMath.mathMod(beginNum, endNum)); //=> NaN 因为被除数不是整数,所以返回 NaN
log(rMath.modulo(negateNum, beginNum)); //=> -2 除数可以是负数。
log(rMath.mathMod(negateNum, beginNum)); //=> 3 除数可以是负数,但是mathMod方法返回值,只会是正数。
const mathModSelf = x => rMath.mathMod(defaultNum, x); // 类似于 defaultNum % x
const selfMathMod = x => rMath.mathMod(x, defaultNum); // 类似于 x % defaultNum
log(arrNum.map(mathModSelf)); //=> [ 0, 1, 2 ]
log(arrNum.map(selfMathMod)); //=> [ 1, 2, 3 ]
objNum.forEach((value, key, map) => {
objNum[key] = mathModSelf(value);
// log(`${key} : ${objNum[key]}`);
});
log(objNum);// Map { 'x' => 1, 'y' => 2, 'z' => 3, x: 0, y: 1, z: 2 }
objNum.forEach((value, key, map) => {
objNum[key] = selfMathMod(value);
// log(`${key} : ${objNum[key]}`);
});
log(objNum);// Map { 'x' => 1, 'y' => 2, 'z' => 3, x: 1, y: 2, z: 3 }
})(console.log)
执行命令
$ node test.js
[Function: RMath]
RMath { author: 'watermelon' }
NaN
-2
3
[ 0, 1, 2 ]
[ 1, 2, 3 ]
Map { 'x' => 1, 'y' => 2, 'z' => 3, x: 0, y: 1, z: 2 }
Map { 'x' => 1, 'y' => 2, 'z' => 3, x: 1, y: 2, z: 3 }
from learn-es6.
Related Issues (20)
- Weekly Digest (26 July, 2020 - 2 August, 2020)
- Weekly Digest (2 August, 2020 - 9 August, 2020)
- learn-MongoDB HOT 9
- learn-java-on-mac
- learn-nginx-on-mac HOT 2
- npm-pug
- npm-koa HOT 1
- learn-docker HOT 1
- npm-koa-bodyparser
- npm-koa-session
- npm-koa-static
- npm-koa-router
- npm-koa-views
- npm-koa-logger
- learn-vscode-ext HOT 3
- npm-nrm HOT 1
- learn-mysql HOT 1
- learn-mac-order HOT 5
- Weekly Digest (12 July, 2020 - 19 July, 2020)
- Weekly Digest (19 July, 2020 - 26 July, 2020)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from learn-es6.