class Sum {
sum(a, b){
return a + b;}}
class Minus {
minus(a, b){
return a - b;}}
class Multiply {
multiply(a, b){
return a * b;}}
class Calculator {
sumObj
minusObj
multiplyObj
constructor(){
this.sumObj= new Sum();
this.minusObj= new Minus();
this.multiplyObj= new Multiply();}
sum(...args){
return this.sumObj.sum(...args);}
minus(...args){
return this.minusObj.minus(...args);}
multiply(...args){
return this.multiplyObj.multiply(...args);}}
let calculator = new Calculator();
console.log(calculator.sum(1,2));
console.log(calculator.minus(1,2));
console.log(calculator.multiply(1,2));
复制代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
4.2 计算机
class CPU {
startup(){ console.log('打开CPU');}
shutdown(){ console.log('关闭CPU');}}
class Memory {
startup(){ console.log('打开内存');}
shutdown(){ console.log('关闭内存');}}
class Disk {
startup(){ console.log('打开硬盘');}
shutdown(){ console.log('关闭硬盘');}}
class Computer {
cpu;
memory;
disk;
constructor(){
this.cpu= new CPU();
this.memory= new Memory();
this.disk= new Disk();}
startup(){
this.cpu.startup();
this.memory.startup();
this.disk.startup();}
shutdown(){
this.cpu.shutdown();
this.memory.shutdown();
this.disk.shutdown();}}
let computer = new Computer();
computer.startup();
computer.shutdown();
复制代码
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
4.3 压缩
export {}
var zlib = require('zlib');
var fs = require('fs');
let path = require('path');
function open(input){
let ext = path.extname(input);
switch (ext){
case '.gz':
return unZip(input);
case '.rar':
return unRar(input);
case '.7z':
return un7z(input);
default:
break;}}
function unZip(src){
var gunzip = zlib.createGunzip();
var inputStream = fs.createReadStream(src);
var outputStream = fs.createWriteStream(src.slice(0,-3));
console.log('outputStream');
inputStream.pipe(gunzip).pipe(outputStream);}
function unRar(src){
console.log('Rar解压后的', src);}
function un7z(src){
console.log('7z解压后的', src);}
open('./source.txt.gz');
function zip(src){
var gzip = zlib.createGzip();//创建压缩流
var inputStream = fs.createReadStream(src);
var outputStream = fs.createWriteStream(src+'.gz');
inputStream.pipe(gzip).pipe(outputStream);}
zip('source.txt');
复制代码
bindReady: function(){// ...
// Mozilla, Opera and webkit 支持
if (document.addEventListener){
document.addEventListener('DOMContentLoaded', DOMContentLoaded,false)// A fallback to window.onload, that will always work
window.addEventListener('load', jQuery.ready,false)// 如果使用了 IE 的事件绑定形式
} else if (document.attachEvent){
document.attachEvent('onreadystatechange', DOMContentLoaded)// A fallback to window.onload, that will always work
window.attachEvent('onload', jQuery.ready)}// ...
}
复制代码