怎么判断是不是new创建的

阅读 52

2022-11-23


function Vue (options) {
if (!(this instanceof Vue)) {
console.warn('Vue is a constructor and should be called with the `new` keyword')
}
// this._init(options)
}

在读vue源码的时候我发现一行代码,还挺有意思的,那就是条件语句中 ​​this instanceof Vue​​ 代码,其实这个代码就是判断该函数只能通过new来调用。


精彩评论(0)

0 0 举报