在函数体里面判断this是否是构造函数的实例即可
Func(){
//
if(!(this instanceof Func)){
console.log("没有用new关键词调用")
}else{
console.log("使用new关键词调用")
}
}
Vue源码
function Vue (options) {
if (process.env.NODE_ENV !== 'production' &&
!(this instanceof Vue)
) {
warn('Vue is a constructor and should be called with the `new` keyword')
}
this._init(options)
}