1 The `proto()` function retrievs or sets the prototype of the given object 2 or resource value. 3 4 Throws an exception if given value does not support setting prototypes. 5 6 When invoked with one argument, returns the prototype of the given value 7 (if any). 8 9 When invoked with two arguments, returns the given value. 10 11 -- Testcase -- 12 {% 13 let fs = require("fs"); 14 15 // create a "class instance" by attaching a function dictionary to 16 // a plain object. 17 let obj = proto({}, { 18 greeting: function(name) { 19 printf("Hello, %s!\n", name); 20 } 21 }); 22 23 // accessing a property on `obj` will look up the prototype chain 24 // if the object itself does not have it 25 obj.greeting("World"); 26 27 printf("%.J\n", [ 28 // retrieve prototype of `fs.file` resource 29 proto(fs.stdout), 30 31 // retrieve prototype of `obj` 32 proto(obj) 33 ]); 34 %} 35 -- End -- 36 37 -- Expect stdout -- 38 Hello, World! 39 [ 40 { 41 "ioctl": "function ioctl(...) { [native code] }", 42 "lock": "function lock(...) { [native code] }", 43 "truncate": "function truncate(...) { [native code] }", 44 "isatty": "function isatty(...) { [native code] }", 45 "error": "function error(...) { [native code] }", 46 "fileno": "function fileno(...) { [native code] }", 47 "flush": "function flush(...) { [native code] }", 48 "close": "function close(...) { [native code] }", 49 "tell": "function tell(...) { [native code] }", 50 "seek": "function seek(...) { [native code] }", 51 "write": "function write(...) { [native code] }", 52 "read": "function read(...) { [native code] }" 53 }, 54 { 55 "greeting": "function(name) { ... }" 56 } 57 ] 58 -- End -- 59 60 61 62 Passing an invalid value throws an exception. 63 64 -- Testcase -- 65 {% 66 proto("inval", {}); 67 %} 68 -- End -- 69 70 -- Expect stderr -- 71 Type error: Passed value is neither a prototype, resource or object 72 In line 2, byte 19: 73 74 ` proto("inval", {});` 75 Near here -----------^ 76 77 78 -- End -- 79 80 -- Testcase -- 81 {% 82 proto({}, "inval"); 83 %} 84 -- End -- 85 86 -- Expect stderr -- 87 Type error: Passed value is neither a prototype, resource or object 88 In line 2, byte 19: 89 90 ` proto({}, "inval");` 91 Near here -----------^ 92 93 94 -- End --
This page was automatically generated by LXR 0.3.1. • OpenWrt