2013-09-01 16 views
6

Với một số đối tượng y, làm thế nào tôi có thể tìm ra cụ thể nhất X như vậy mà biểu hiệnCách nhận được X cụ thể nhất sao cho instanceof của y là đúng?

y instanceof X 

đánh giá để true?

Ví dụ, hai biểu thức sau cả hai đánh giá là true

[] instanceof Object 
[] instanceof Array 

... nhưng Array là cụ thể hơn so Object.

+3

Tôi tin đó là 'y.constructor'. –

+0

Ngoài ra [Làm thế nào để có được một lớp của đối tượng JavaScript?] (Http://stackoverflow.com/questions/1249531/how-to-get-a-javascript-objects-class) –

+0

http://stackoverflow.com/questions/ 1249531/how-to-get-a-javascript-đối tượng-class – ash

Trả lời

1

Theo MDN:

Các instanceof kiểm tra hành dù một đối tượng có trong chuỗi nguyên mẫu của nó thuộc tính prototype của một nhà xây dựng.

Vì vậy, Kolink chính xác là việc kiểm tra y.constructor sẽ giúp bạn trả lời. (Lưu ý rằng điều này giả định bạn hoặc ai đó có không đọc sai y.constructor trong khi chờ đợi, tức là rằng y 's __proto__ (đầu chuỗi nguyên mẫu của nó) thực sự chỉ đến y.constructor.prototype)

0

Dưới đây là một mã Tôi vừa mới viết điều đó thực hiện công việc. Đọc nhận xét để biết thêm chi tiết!

Lưu ý: Tôi đã thử nghiệm đoạn mã này trong Google Chrome!

function compareTypes(a,b){ 
    if (a instanceof b) { 
     if(b instanceof a) { 
      /* same types */ 
      return 0; 
     } 
     else { 
      /* first is son */ 
      return -1; 
     } 
    } 
    if (b instanceof a) { 
     /* second is son */ 
     return 1; 
    } 
    return 0; /* not related to each other */ 
} 

function closestInstanceOf(obj, possibleTypesAsAdditionalArguments) { 
    /* 
    * How to call this function: 
    * The first argument should be the object to check. 
    * All other arguments are possible types. 
    */ 

    if (arguments.length === 0) 
     return; /* undefined */ 

    if (arguments.length === 1) 
     return Object; /* If no possible type was passed, we returned Object as the base type */ 

    /* converts the arguments to Javascript array */ 
    var types = Array.prototype.slice.call(arguments, 0); 

    var obj = types[0]; /* object to be checked */ 
    types.splice(0, 1); /* possible types */ 

    /* Sorts the types array from the more specific to the more generic type */ 
    types.sort(compareTypes); 

    /* find the first match */ 
    for (var i = 0; i < types.length; i++) { 
     var type = types[i]; 
     if (obj instanceof type) { 
      return type; 
     } 
    } 
    return Object; 
} 

// examples 

// 1. Sorting types 
console.info([Array, Number, Object, String, Boolean].sort(compareTypes)); 
// results: 
//[function, function, function, function, function] 
// 0: function Array() { [native code] } 
// 1: function Number() { [native code] } 
// 2: function String() { [native code] } 
// 3: function Boolean() { [native code] } 
// 4: function Object() { [native code] } 

// 2. Find specific type 
console.info(closestInstanceOf([], Array, Number, Object, String, Boolean)); 
// results: 
// function Array() { [native code] } 

// 3. Find specific type 
console.info(closestInstanceOf(new String("Hello World"), Array, Number, Object, String, Boolean)); 
// results: 
// function String() { [native code] } 

// 4. Find specific type 
console.info(closestInstanceOf(new Number(19), Array, Number, Object, String, Boolean)); 
// results: 
// function Number() { [native code] } 

// 5. Find specific type 
console.info(closestInstanceOf("Hello world", Array, Number, Object, String, Boolean)); 
// results: 
// function Object() { [native code] } 
// Note: String iterals derived from object! 

// 6. Find specific type 
console.info(closestInstanceOf(18, Array, Number, Object, String, Boolean)); 
// results: 
// function Object() { [native code] } 
// The same applies for numbers 

// 7. Find specific type 
console.info(closestInstanceOf(false, Array, Number, Object, String, Boolean)); 
// results: 
// function Object() { [native code] } 
// And booleans... 

// 8. Find specific type 
console.info(closestInstanceOf(new Boolean(false), Array, Number, Object, String, Boolean)); 
// results: 
// function Object() { [native code] } 
// Again, using new Boolean does the job! 


// 9. Find specific type 
console.info(closestInstanceOf(function() { /* this is custom class */ }, Array, Number, Object, String, Boolean)); 
// results: 
// function Object() { [native code] } 
// This code works for custom class as well, try it! 
Các vấn đề liên quan