#454 ✓fixed_in_master_branch
Juriy Zaytsev

Object.keys to match ES3.1 specs more closely

Reported by Juriy Zaytsev | November 20th, 2008 @ 04:55 AM | in 2.0

Something among these lines:


(function(){

  var rePrimitiveType = /^(string|number|boolean)$/;
  var hasOwnProperty = Object.prototype.hasOwnProperty;

  function isPrimitive(object) {
    return object == null || rePrimitiveType.test(typeof object);
  }

  function keys(object, fast) {
    if (isPrimitive(object)) {
      throw TypeError();
    }
    var result = [];
    for (var prop in object) {
      if (hasOwnProperty.call(object, prop)) {
        result.push(prop);
      }
    }
    return !!fast ? result : result.sort();
  }

  Object.keys = keys;

})();

This requires hasOwnProperty (which we can simulate for Safari 2) and might be unnecessary slow - e.g. type checking (just for the sake of spec conformance).

Thoughts?

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

Pages