1 | ## our_shell: ysh
|
2 | ## oils_failures_allowed: 2
|
3 |
|
4 | #### Object() creates prototype chain
|
5 |
|
6 | func Rect_area(this) {
|
7 | return (this.x * this.y)
|
8 | }
|
9 |
|
10 | var Rect = Object(null, {area: Rect_area})
|
11 |
|
12 | var rect1 = Object(Rect, {x: 3, y: 4})
|
13 | var rect2 = Object(Rect, {x: 10, y: 20})
|
14 |
|
15 | # This could change to show the object?
|
16 | # pp test_ (rect)
|
17 |
|
18 | # TODO: This should be a bound function
|
19 | #pp asdl_ (rect)
|
20 | #pp (rect.area)
|
21 | #pp (rect->area)
|
22 |
|
23 | var area1 = rect1.area()
|
24 | var area2 = rect2.area()
|
25 |
|
26 | pp test_ ([rect1.x, rect1.y])
|
27 | echo "area1 = $area1"
|
28 |
|
29 | pp test_ ([rect2.x, rect2.y])
|
30 | echo "area2 = $area2"
|
31 |
|
32 | #pp test_ (rect1.nonexistent)
|
33 |
|
34 | ## STDOUT:
|
35 | (List) [3,4]
|
36 | area1 = 12
|
37 | (List) [10,20]
|
38 | area2 = 200
|
39 | ## END
|
40 |
|
41 | #### can't encode objects as JSON
|
42 |
|
43 | var Rect = Object(null, {})
|
44 |
|
45 | json write (Rect)
|
46 | echo 'nope'
|
47 |
|
48 | ## status: 1
|
49 | ## STDOUT:
|
50 | ## END
|
51 |
|
52 | #### pretty printing of cycles
|
53 |
|
54 | var d = {k: 42}
|
55 | setvar d.cycle = d
|
56 |
|
57 | pp test_ (d)
|
58 |
|
59 | var o = Object(null, d)
|
60 |
|
61 | pp test_ (o)
|
62 |
|
63 | var o2 = Object(o, {z: 99})
|
64 |
|
65 | pp test_ (o2)
|
66 |
|
67 | ## STDOUT:
|
68 | ## END
|
69 |
|
70 | #### setvar obj.attr
|
71 |
|
72 | func Rect_area(this) {
|
73 | return (this.x * this.y)
|
74 | }
|
75 |
|
76 | var Rect = Object(null, {area: Rect_area})
|
77 |
|
78 | var rect1 = Object(Rect, {x: 3, y: 4})
|
79 |
|
80 | pp test_ (rect1)
|
81 |
|
82 | # Right now it's not mutable
|
83 | setvar rect1.x = 99
|
84 |
|
85 | pp test_ (rect1)
|
86 |
|
87 | ## STDOUT:
|
88 | (Obj) {"x":3,"y":4} ==> {"area":<Func>}
|
89 | ## END
|
90 |
|
91 | #### Can all builtin methods with s.upper()
|
92 |
|
93 | var s = 'foo'
|
94 | var x = s.upper()
|
95 | var y = "--$[x.lower()]"
|
96 |
|
97 | pp test_ (x)
|
98 | pp test_ (y)
|
99 |
|
100 | # TODO:
|
101 | # keys(d) values(d) instead of d.keys() and d.values()
|
102 | #
|
103 | # mutating methods are OK?
|
104 | # call d->inc(x)
|
105 |
|
106 | ## STDOUT:
|
107 | (Str) "FOO"
|
108 | (Str) "--foo"
|
109 | ## END
|