Skip to content

Commit edb1798

Browse files
committed
Object.create docs
1 parent 1e2e296 commit edb1798

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

src/Core__Object.res

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,24 @@ fruit === {"name": "Apple" } // false
4141
@val
4242
external is: ('a, 'a) => bool = "Object.is"
4343

44-
@val external create: {..} => {..} = "Object.create"
45-
@val external createWithProperties: ({..}, {..}) => {..} = "Object.create"
44+
/**
45+
`create` creates a new object, using an existing object as the prototype of the new object. See [Object.create on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create)
46+
47+
**Note:** ReScript provides [first-class support for immutable objects](https://rescript-lang.org/docs/manual/latest/object) and [records](https://rescript-lang.org/docs/manual/latest/record). This is often safer and more convenient than using `create` and other functions in this module.
48+
49+
## Examples
50+
51+
```rescript
52+
let x = {"fruit": "banana"}
53+
let y = Object.create(x)
54+
y->Object.get("fruit") // Some("banana")
55+
```
56+
*/
57+
@val
58+
external create: {..} => {..} = "Object.create"
59+
60+
@val
61+
external createWithProperties: ({..}, {..}) => {..} = "Object.create"
4662
@val external createWithNull: (@as(json`null`) _, unit) => {..} = "Object.create"
4763
@val external createWithNullAndProperties: (@as(json`null`) _, {..}) => {..} = "Object.create"
4864

test/ObjectTests.mjs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,30 @@ Test.run([
599599
"Object.getSymbol when not exists return it as None"
600600
], ({})[Symbol("fruit")], eq, undefined);
601601

602+
Test.run([
603+
[
604+
"ObjectTests.res",
605+
168,
606+
13,
607+
46
608+
],
609+
"Object.create clones properties"
610+
], Object.create({
611+
a: 1
612+
})["a"], eq, 1);
613+
614+
Test.run([
615+
[
616+
"ObjectTests.res",
617+
175,
618+
13,
619+
46
620+
],
621+
"Object.create clones properties"
622+
], Object.create({
623+
a: 1
624+
})["b"], eq, undefined);
625+
602626
export {
603627
eq ,
604628
nums ,

test/ObjectTests.res

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,20 @@ Test.run(
161161
eq,
162162
None,
163163
)
164+
165+
// ===== create =====
166+
167+
Test.run(
168+
__POS_OF__(`Object.create clones properties`),
169+
{"a": 1}->Object.create->Object.get("a"),
170+
eq,
171+
Some(1),
172+
)
173+
174+
Test.run(
175+
__POS_OF__(`Object.create clones properties`),
176+
{"a": 1}->Object.create->Object.get("b"),
177+
eq,
178+
None,
179+
)
180+

0 commit comments

Comments
 (0)