Skip to content

Commit

Permalink
fixes IE11's lack of Object.getPrototypeOf working on primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbmeyer committed Jun 14, 2018
1 parent 4754791 commit 5d463c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions reflections/shape/shape-test.js
Expand Up @@ -544,6 +544,8 @@ QUnit.test("hasKey", function() {
QUnit.ok(shapeReflections.hasKey(55, "toFixed") , "works on primitives");
QUnit.ok(shapeReflections.hasKey(true, "valueOf") , "works on primitives");
QUnit.ok(shapeReflections.hasKey('foo', "length") , "works on primitives");
QUnit.notOk(shapeReflections.hasKey(null, "length") , "works on null");
QUnit.notOk(shapeReflections.hasKey(undefined, "length") , "works on undefined");
});

QUnit.test("serialize clones", function(){
Expand Down
18 changes: 15 additions & 3 deletions reflections/shape/shape.js
Expand Up @@ -3,6 +3,16 @@ var getSetReflections = require("../get-set/get-set");
var typeReflections = require("../type/type");
var helpers = require("../helpers");



var getPrototypeOfWorksWithPrimitives = true;
try {
Object.getPrototypeOf(1);
} catch(e) {
getPrototypeOfWorksWithPrimitives = false;
}


var ArrayMap;
if(typeof Map === "function") {
ArrayMap = Map;
Expand Down Expand Up @@ -982,15 +992,17 @@ shapeReflections = {
* @param {String} key The key to look up on `obj`
* @return {Boolean} `true` if `obj`'s key set contains `key` or an object on its prototype chain's key set contains `key`, `false` otherwise
*/
"hasKey": function(obj, key) {
hasKey: function(obj, key) {
if( obj == null ) {
return false;
}
if (typeReflections.isPrimitive(obj)) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
return true;
} else {
return key in Object.getPrototypeOf(obj);
return key in (getPrototypeOfWorksWithPrimitives ? Object.getPrototypeOf(obj) : obj.__proto__);
}
}

var hasKey = obj[canSymbol.for("can.hasKey")];
if(hasKey) {
return hasKey.call(obj, key);
Expand Down

0 comments on commit 5d463c2

Please sign in to comment.