From ae660d19216d0aff585e3caa27a7b585d3f0fe0a Mon Sep 17 00:00:00 2001 From: Harvey Date: Sat, 11 Mar 2023 18:53:51 +0800 Subject: [PATCH] Huauauaa/cheat-sheet#73 defineProperty --- test/prototype.test.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 test/prototype.test.js diff --git a/test/prototype.test.js b/test/prototype.test.js new file mode 100644 index 0000000..e883613 --- /dev/null +++ b/test/prototype.test.js @@ -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 '#' + // foo.id = 2; + foo.name = 'foo1'; + assert.strictEqual(foo.id, 1); + assert.strictEqual(foo.name, 'foo1'); + }); +});