calss只能使用new调用,不能普通调用(cannot be invoked without ‘new’);
构造函数有两种来调用方法,普通调用和new调用
class中的原型方法是不可枚举的,构造函数的原型方法可枚举
class中的所有代码在严格模式下,构造函数中的代码在普通模式下
class中的原型方法不能通过new调用,构造函数中原型方法可以用new调用
//class寫法classComputer1{constructor(name, price){this.name = name;this.price = price;}//原型方法,通過實例調用showPrice(){
console.log(`這台${this.name}電腦的價格是${this.price}`);}//類的靜態方法,通過類調用staticstaticFun(){
console.log("這是class的靜態方法");}}("use strict");functionComputer(name, age){if(!new.target){thrownewError("Computer cannot be invoked without new");}this.name = name;this.age = age;}
Object.defineProperty(Computer.prototype,"showPrice",{value:function(){if(new.target){thrownewError("showPrice is not a constructor");}},enumerable:false,});