functionAnimal(name) { // 2. 只能通过 new 关键字创建,不能够直接调用 if (!new.target) { thrownewTypeError(`Class constructor ${this.constructor.name} cannot be invoked without 'new'`) } this.name = name }
Object.defineProperty(Animal.prototype, 'run', { value: function() { // 4. 不能通过 new 关键字创建 if (new.target) { thrownewTypeError(`Animal.prototype.run cannot be invoked with 'new'`) } console.log(this.name + ' is running!') }, // 3. 当前属性方法不能够枚举 enumerable: false })