我为有效的电话号码编写代码。但是当我编译项目时,我有以下错误:
- 无法获取属性“块”的值:对象为空或未定义;
命令的输出 ""C:\Program Files (x86)\Microsoft SDKs\TypeScript\tsc" "C:..\Scripts\MyScripts\TS\BuildPhone.ts" "C:..\Scripts\MyScripts\TS\ProductsViewModel .ts" "C:..\Scripts\MyScripts\TS\helloWorld.ts"" 代码为 1。
// Interface interface IPhoneBuild { showPhone(): string; checkPhone(): boolean; } class Phone { Code: string; Number: string; Fax: boolean; } // Module module Phones { // Class export class PhoneBuild implements IPhoneBuild { private phone: Phone; private codes: string[]; // Constructor constructor(public Number: string, public Code: string, public Codes: string[]) { this.phone = this.buildPhone(Number, Code); this.codes = Codes; } //Private Methods private clearPhone(public reg: string) { var re = /\W/g; return reg.replace(re, ""); } private buildPhone(public num: string, public code: string) { var p: Phone = { Code: "", Number: "", Fax: false }; num = this.clearPhone(num); if (num.length == 6) { p = { Code: code, Fax: false, Number: num }; } if (num.length == 11) { p = { Code: num.substring(0, 4), Fax: false, Number: num.substring(4)}; } return p; } // Instance member public showPhone() { return this.phone.Code + this.phone.Number; } public checkPhone() { return this.phone.Number.length != 7 || this.phone.Code.length == 0; } } }
你有什么想法?我需要帮助。
我找到了解决方法:在方法中的属性中删除“公共”:buildPhone 和 clearPhone。