Skip to content

Commit

Permalink
[Fix] es6-sham: Function.prototype.name: don’t poison the getter …
Browse files Browse the repository at this point in the history
…when the receiver is `Function.prototype`

Fixes #454.
  • Loading branch information
ljharb committed Mar 7, 2019
1 parent e8a828d commit 003ee5d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
3 changes: 3 additions & 0 deletions es6-sham.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@
configurable: true,
enumerable: false,
get: function () {
if (this === Function.prototype) {
return '';
}
var str = _call(functionToString, this);
var match = _call(_strMatch, str, /\s*function\s+([^(\s]*)\s*/);
var name = match && match[1];
Expand Down
6 changes: 6 additions & 0 deletions test-sham/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ describe('Function', function () {
});
});

it('does not poison every name when accessed on Function.prototype', function () {
expect((function foo() {}).name).to.equal('foo');
expect(Function.prototype.name).to.equal('');
expect((function foo() {}).name).to.equal('foo');
});

it('returns empty string for anonymous functions', function () {
var anon = identity(function () {});
expect(anon.name).to.equal('');
Expand Down
12 changes: 6 additions & 6 deletions test-sham/set-prototype-of.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ describe('Object.setPrototypeOf(o, p)', function () {

it('changes prototype to regular objects', function () {
var obj = { a: 123 };
expect(obj).to.be.an.instanceOf(Object);
expect(obj instanceof Object).to.equal(true);
// sham requires assignment to work cross browser
obj = Object.setPrototypeOf(obj, null);
expect(obj).not.to.be.an.instanceOf(Object);
expect(obj instanceof Object).to.equal(false);
expect(obj.a).to.equal(123);
});

it('changes prototype to null objects', function () {
var obj = Object.create(null);
obj.a = 456;
expect(obj).not.to.be.an.instanceOf(Object);
expect(obj instanceof Object).to.equal(false);
expect(obj.a).to.equal(456);
// sham requires assignment to work cross browser
obj = Object.setPrototypeOf(obj, {});
expect(obj).to.be.an.instanceOf(Object);
obj = Object.setPrototypeOf(obj, { b: 789 });
expect(obj instanceof Object).to.equal(true);
expect(obj.a).to.equal(456);
expect(obj.b).to.equal(789);
});

});

0 comments on commit 003ee5d

Please sign in to comment.