นี่เป็นเพียงความรู้เสริมสำหรับคำอธิบายอื่น ๆ ทั้งหมดที่นี่ - ฉันไม่แนะนำให้ใช้.constructorทุกที่
TL; DR:ในสถานการณ์ที่typeofไม่ได้เป็นตัวเลือกและเมื่อคุณรู้ว่าคุณไม่สนใจเกี่ยวกับห่วงโซ่ต้นแบบ , Object.prototype.constructorอาจจะเป็นที่ทำงานหรือแม้กระทั่งการเลือกที่ดีกว่าinstanceof:
x instanceof Y
x.constructor === Y
มันอยู่ในมาตรฐานตั้งแต่ 1.1 ดังนั้นไม่ต้องกังวลเกี่ยวกับความเข้ากันได้ย้อนหลัง
มูฮัมหมัดอูเมอร์พูดสั้น ๆ นี้ในความคิดเห็นที่นี่ด้วย มันทำงานได้กับทุกอย่างด้วยต้นแบบ - ดังนั้นทุกอย่างไม่ได้nullหรือundefined:
// (null).constructor;      // TypeError: null has no properties
// (undefined).constructor; // TypeError: undefined has no properties
(1).constructor;                 // function Number
''.constructor;                  // function String
([]).constructor;                // function Array
(new Uint8Array(0)).constructor; // function Uint8Array
false.constructor;               // function Boolean()
true.constructor;                // function Boolean()
(Symbol('foo')).constructor;     // function Symbol()
// Symbols work, just remember that this is not an actual constructor:
// new Symbol('foo'); //TypeError: Symbol is not a constructor
Array.prototype === window.frames.Array;               // false
Array.constructor === window.frames.Array.constructor; // true
ยิ่งกว่านั้นขึ้นอยู่กับกรณีการใช้งานของคุณมันอาจเร็วกว่ามากinstanceof (สาเหตุที่เป็นไปได้ที่ไม่ต้องตรวจสอบโซ่ต้นแบบทั้งหมด) ในกรณีของฉันฉันต้องการวิธีที่รวดเร็วในการตรวจสอบว่าค่าเป็นอาร์เรย์ที่พิมพ์หรือไม่:
function isTypedArrayConstructor(obj) {
  switch (obj && obj.constructor){
    case Uint8Array:
    case Float32Array:
    case Uint16Array:
    case Uint32Array:
    case Int32Array:
    case Float64Array:
    case Int8Array:
    case Uint8ClampedArray:
    case Int16Array:
      return true;
    default:
      return false;
  }
}
function isTypedArrayInstanceOf(obj) {
  return obj instanceof Uint8Array ||
    obj instanceof Float32Array ||
    obj instanceof Uint16Array ||
    obj instanceof Uint32Array ||
    obj instanceof Int32Array ||
    obj instanceof Float64Array ||
    obj instanceof Int8Array ||
    obj instanceof Uint8ClampedArray ||
    obj instanceof Int16Array;
}
https://run.perf.zone/view/isTypedArray-constructor-vs-instanceof-1519140393812
และผลลัพธ์:
Chrome 64.0.3282.167 (64 บิต, Windows)

Firefox 59.0b10 (64- บิต, Windows)

ออกจาก curiousity ฉันไม่เป็นมาตรฐานของเล่นอย่างรวดเร็วกับtypeof; น่าประหลาดใจที่มันทำงานได้ไม่ดีกว่ามากและดูเหมือนว่าเร็วขึ้นเล็กน้อยใน Chrome:
let s = 0,
    n = 0;
function typeofSwitch(t) {
    switch (typeof t) {
        case "string":
            return ++s;
        case "number":
            return ++n;
        default:
            return 0;
    }
}
// note: no test for null or undefined here
function constructorSwitch(t) {
    switch (t.constructor) {
        case String:
            return ++s;
        case Number:
            return ++n;
        default:
            return 0;
    }
}
let vals = [];
for (let i = 0; i < 1000000; i++) {
    vals.push(Math.random() <= 0.5 ? 0 : 'A');
}
https://run.perf.zone/view/typeof-vs-constructor-string-or-number-1519142623570
หมายเหตุ: ลำดับการทำงานของฟังก์ชั่นที่ปรากฏในรายการระหว่างภาพ!
Chrome 64.0.3282.167 (64 บิต, Windows)

Firefox 59.0b10 (64- บิต, Windows)
หมายเหตุ: ลำดับการทำงานของฟังก์ชั่นที่ปรากฏในรายการระหว่างภาพ!
