美文网首页我爱编程
乱入的TypeScript

乱入的TypeScript

作者: 爱斯基摩白 | 来源:发表于2018-08-07 18:58 被阅读0次

简单的说TypeScript是强类型(强类型就是对类型更细致的区分),类似于java的string和int类型, 这在javascript中是没有的,所以称javascript是弱类型,比如var a;
在javascript中通用var类型,下面是一个简单的计算器Demo。

非面向对象
{
//初始化数据
let n1: string = '';
let n2: string = '';
let operator: string = '';
let result: string = '';

//创建容器
let calc = document.createElement('div');
calc.classList.add('calc');
document.body.appendChild(calc);

// 创建界面
let output = document.createElement('div');
output.classList.add('output');
let span = document.createElement('span');
output.appendChild(span);
span.textContent = '0';
calc.appendChild(output);

// 定义getResult
let getResult = (n1: string, n2: string, operator: string): string => {
let numberN1: number = parseInt(n1);
let numberN2: number = parseInt(n2);
if (operator === '+') {
return (numberN1 + numberN2).toString();
} else if (operator === '-') {
return (numberN1 - numberN2).toString();
} else if (operator === '×') {
return (numberN1 * numberN2).toString();
} else if (operator === '÷') {
return (numberN1 / numberN2).toString();
}
};

//添加事件监听
calc.addEventListener('click', event => {
if (event.target instanceof HTMLButtonElement) {
const text = event.target.textContent;
if ('0123456789'.indexOf(text) >= 0) {
if (operator) {
n2 += text;
span.textContent = n2;
} else {
result = '';
n1 += text;
span.textContent = n1;
}
} else if ('+-×÷'.indexOf(text) >= 0) {
if (result) {
n1 = result;
result = '';
}
operator = text;
} else if ('='.indexOf(text) >= 0) {
result = getResult(n1, n2, operator);
span.textContent = result;
n1 = '';
n2 = '';
operator = '';
} else {
console.log('不晓得');
}
console.log(n1, operator, n2);
}
});

//定义createButton
let createButton = (text: string | number, container: HTMLElement, className?: string) => {
let button = document.createElement('button');
button.textContent = text.toString();
className && button.classList.add(className);
container.appendChild(button);
};
// 初始化按钮
let textLists: Array<Array<string | number>> = [
['Clear', '÷'],
[7, 8, 9, '×'],
[4, 5, 6, '-'],
[1, 2, 3, '+'],
[0, '.', '='],
];
// 循环创建按钮
textLists.forEach((textList: Array<number | string>) => {
let div = document.createElement('div');
div.classList.add('row');
textList.forEach((text: number | string) => {
createButton(text, div, text-${text});
});
calc.appendChild(div);
});
}

面向对象
{
class Calculator {
public n1: string = '';
public n2: string = '';
public operator: string = '';
public result: string = '';
public span: HTMLSpanElement;
public container: HTMLDivElement;
public textLists: Array<Array<string | number>> = [
['Clear', '÷'],
[7, 8, 9, '×'],
[4, 5, 6, '-'],
[1, 2, 3, '+'],
[0, '.', '='],
];
constructor() {
this.createContainer();
this.createElements();
this.createButtons();
this.bindEvents();
}
createContainer() {
let calc = document.createElement('div');
calc.classList.add('calc');
document.body.appendChild(calc);
this.container = calc;
}
createElements() {
let output = document.createElement('div');
output.classList.add('output');
let span = document.createElement('span');
output.appendChild(span);
span.textContent = '0';
this.span = span;
this.container.appendChild(output);
}
createButtons() {
this.textLists.forEach((textList: Array<number | string>) => {
let div = document.createElement('div');
div.classList.add('row');
textList.forEach((text: number | string) => {
this.createButton(text, div, text-${text});
});
this.container.appendChild(div);
});
}
bindEvents() {
this.container.addEventListener('click', event => {
if (event.target instanceof HTMLButtonElement) {
const text = event.target.textContent;
if ('0123456789.'.indexOf(text) >= 0) {
if (this.operator) {
this.n2 += text;
this.span.textContent = this.n2;
} else {
this.result = '';
this.n1 += text;
this.span.textContent = this.n1;
}
} else if ('+-×÷'.indexOf(text) >= 0) {
if (this.result) {
this.n1 = this.result;
this.result = '';
}
this.operator = text;
} else if ('='.indexOf(text) >= 0) {
this.result = this.removeZero(this.getResult(this.n1, this.n2, this.operator));
this.span.textContent = this.result;
this.n1 = '';
this.n2 = '';
this.operator = '';
} else if (text === 'Clear') {
this.n1 = '';
this.n2 = '';
this.operator = '';
this.result = '';
this.span.textContent = '0';
}
console.log(this.n1, this.operator, this.n2);
}
});
}
removeZero(string) {
return string.replace(/.0+$/g, '').replace(/.0+e/, 'e');
}
getResult(n1: string, n2: string, operator: string): string {
let numberN1: number = parseFloat(n1);
let numberN2: number = parseFloat(n2);
if (operator === '+') {
return (numberN1 + numberN2).toPrecision(12);
} else if (operator === '-') {
return (numberN1 - numberN2).toPrecision(12);
} else if (operator === '×') {
return (numberN1 * numberN2).toPrecision(12);
} else if (operator === '÷') {
if (numberN2 === 0) {
return '不是数字';
} else {
return (numberN1 / numberN2).toPrecision(12);
}
}
}
createButton(text: string | number, container: HTMLElement, className?: string) {
let button = document.createElement('button');
button.textContent = text.toString();
className && button.classList.add(className);
container.appendChild(button);
}
}

new Calculator();
}

相关文章

  • 乱入的TypeScript

    简单的说TypeScript是强类型(强类型就是对类型更细致的区分),类似于java的string和int类型, ...

  • 入坑TypeScript(一):使用TypeScript写nod

    入坑TypeScript(一):使用TypeScript写node脚本爬取小说 一、为何入坑TypeScript?...

  • TypeScript入坑

    TypeScript 入坑 安装使用npm安装 使用全局方式安装 npm install -g typescrip...

  • 乱入

  • 乱入

    睹月思王王不归 借酒浇愁愁难醉 衣带渐宽终不悔 为伊消得人憔悴

  • 乱入

    生活是怎样?拿着手机看着关于别人的消息:有新婚、有添子是喜;有生离死别是悲;还有些没事在‘作’事的是啥子呢?可那都...

  • 乱入

    看到好多简友在画画,我也想凑凑热闹,第一天画画打卡

  • 乱入

    曾梦见,异星大军入侵我的领地,我的国家被侵占,子民流落他乡,我也隐入深山,唯一信任的人,也被埋葬在高山之巅,就此流...

  • 乱入

    曾梦见,异星大军入侵我的领地,我的国家被侵占,子民流落他乡,我也隐入深山,唯一信任的人,也被埋葬在高山之巅,就此流...

  • 乱入

    想问,这个世界,能不能抱有一颗宁静致远的心而又同时有一颗时刻忧患紧抓机遇向前冲的心。 紧跟世界步伐又怕被时代吞噬 ...

网友评论

    本文标题:乱入的TypeScript

    本文链接:https://www.haomeiwen.com/subject/pvnavftx.html