程序员(web前端开发工程师)、手机号码、二进制、十进制、构造函数、substr、parseInt、prototype、length

function PhoneNumber() {
    this.arrayNumber = ['110', '10', '111', '11', '0', '1000', '100', '1', '101', '1001'];
    this.i = 0;
    this.len = 0;
    this.result = '';
}
PhoneNumber.prototype.calculation = function (params = '') {
    this.len = params.length;

    if (!this.len) return '长度不能为空!';
    if (!/^\d+$/.test(Number(params))) return '请输入纯数字!';

    for (; this.i < this.len;)(this.result += parseInt(this.arrayNumber[params[this.i]], 2), this.i++);

    this.result = `${this.result.substr(0, 3)} ${this.result.substr(3, 4)} ${this.result.substr(7)}`;

    return this.result;
}

let phoneNumber = new PhoneNumber();
console.log(phoneNumber.calculation('78159051872'));

你可能感兴趣的:(JavaScript,web,前端,javascript)