我正在使用以下代码在 JavaScript 中定义一个类...
// file_a.js
function class_a() {
this.prop1 = null;
this.prop2 = null;
}
// file_b.js
var obj = new class_a;
// I need to check here if class_a exists
我该如何做到这一点?
敬礼
我正在使用以下代码在 JavaScript 中定义一个类...
// file_a.js
function class_a() {
this.prop1 = null;
this.prop2 = null;
}
// file_b.js
var obj = new class_a;
// I need to check here if class_a exists
我该如何做到这一点?
敬礼
if (typeof class_a === 'function')
class_a
还没有被定义,那么它将是undefined
,所以typeof class_a
将会是字符串'undefined'
,因此===
测试将会评估为false
,这是正确的答案。它完美地工作。 - Quentin