Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #11584: 0014-More-robust-Object.keys.patch

File 0014-More-robust-Object.keys.patch, 2.2 kB (added by kangax, 5 months ago)
  • a/src/base.js

    old new  
    3737    var ancestor   = this.superclass && this.superclass.prototype; 
    3838    var properties = Object.keys(source); 
    3939     
    40     if (!Object.keys({ toString: true }).length) 
    41       properties.push("toString", "valueOf"); 
    42      
    4340    for (var i = 0, length = properties.length; i < length; i++) { 
    4441      var property = properties[i], value = source[property]; 
    4542      if (ancestor && Object.isFunction(value) && 
     
    109106    return object && object.toHTML ? object.toHTML() : String.interpret(object); 
    110107  }, 
    111108   
    112   keys: function(object) { 
    113     var keys = []; 
    114     for (var property in object) 
    115       keys.push(property); 
    116     return keys; 
    117   }, 
     109  keys: (function() { 
     110    var isDontEnumSkipped = true; 
     111    var DontEnumProperties = [ 
     112      'toString',  
     113      'toLocaleString', 
     114      'valueOf',  
     115      'hasOwnProperty',  
     116      'isPrototypeOf',  
     117      'propertyIsEnumerable' 
     118    ]; 
     119    var length = DontEnumProperties.length; 
     120    var hasOwnProperty = Object.prototype.hasOwnProperty; 
     121     
     122    // IE does not enumerate over properties of an object 
     123    // if there is a corresponding DontEnum property in a prototype chain 
     124    for (var prop in { toString: true }) { 
     125      isDontEnumSkipped = false; 
     126    }; 
     127    return function() { 
     128      var keys = []; 
     129      for (var property in object) { 
     130        keys.push(property); 
     131      } 
     132      // if DontEnum is buggy, we check whether any  
     133      // of the Object.prototype.* properties are "directly" in the object 
     134      if (isDontEnumSkipped) { 
     135        for (var i=0; i<length; i++) { 
     136          if (hasOwnProperty.call(object, DontEnumProperties[i])) 
     137            keys.push(DontEnumProperties[i]); 
     138        } 
     139      } 
     140      return keys; 
     141    } 
     142  })(), 
    118143   
    119144  values: function(object) { 
    120145    var values = [];