Skip to content

Commit

Permalink
Huauauaa/cheat-sheet#73 defineProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
Huauauaa committed Mar 11, 2023
1 parent 0afd420 commit ae660d1
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions test/prototype.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as assert from 'assert';

describe('prototype', function () {
const foo = { id: 1, name: 'foo' };
foo.__proto__.bar = 'bar';
it('for in', function () {
for (const key in foo) {
console.log(key);
}
assert.strictEqual(foo.bar, 'bar');
});
it('keys', function () {
Object.keys(foo).forEach((key) => console.log(key));
});

it('writable', function () {
Object.defineProperty(foo, 'id', { writable: false });
// TypeError: Cannot assign to read only property 'id' of object '#<Object>'
// foo.id = 2;
foo.name = 'foo1';
assert.strictEqual(foo.id, 1);
assert.strictEqual(foo.name, 'foo1');
});
});

0 comments on commit ae660d1

Please sign in to comment.