Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Prototyping

This might be some restriction in the API, but I can't seem to prototype functions: <a href="https://gist.github.com/e-crow-7/7b409dd23f16923bb" rel="nofollow">https://gist.github.com/e-crow-7/7b409dd23f16923bb</a>... Normal?
I guess I should have looked at NodeJS a bit more. following this link <a href="https://howtonode.org/prototypical-inheritance" rel="nofollow">https://howtonode.org/prototypical-inheritance</a> It has to be done like this? MyObject.prototype = { z: function () { log("Hello world, my name is "); } }; weird.
1490075616

Edited 1490075846
Lithl
Pro
Sheet Author
API Scripter
The log function in the API runs JSON.stringify on the argument, unlike console.log. w and z in your gist are both functions, which aren't valid JSON, so they come out undefined. See it in an interactive console, for example: &gt; JSON.stringify(() =&gt; ({})) &lt; undefined JSON.stringify is also used on values stored in the state object when it's serialized, meaning you can't store functions directly in state. In both cases, you can call toString on the function to log (or store) the string representation of the function: &gt; JSON.stringify((() =&gt; ({})).toString()) &lt; ""() =&gt; ({})""
1490096130

Edited 1490097353
Thanks. That is good information on the logging. Especially since debugging seems limited. I guess an alternative way to log is to use your example to make a convenient function like this: function alternativelog(text) { &nbsp;&nbsp;&nbsp; log( text ? JSON.stringify(text.toString()).replace(/^"(.*)"$/, '$1') : text ); } Still, mildly frustrates me that this results in an error though. In my example, z was actually an undefined function. While w wasn't. function MyObject() { this.w = function MyFunc(){ log("entered MyFunc()"); }; } MyObject.prototype.z = function Testing(){ log("entered Testing()"); }; var my_object = new MyObject(); my_object.w(); // Good. my_object.z(); // TypeError: my_object.z is not a function. --------------------- Know of a good way to output the contents of the prototype like console.log()? log( MyObject.prototype );
1490115800
Lithl
Pro
Sheet Author
API Scripter
Hmm, that's odd. I know directly mutating function prototypes is possible; there's two one-click install scripts I've authored which mutate String.prototype. Have you tried using an ES6 class instead of a function prototype?
1490120106

Edited 1490125623
Haven't tried ES6 classes before, but that is working for me. About the prototyping, It seems to be perfectly working now. Either I'm growing crazy or something definitely changed on the back-end. I'm more towards the latter. I'm getting inconsistent errors time and time again. Either way, I think the ES6 classes will satisfy me much more. I appreciate it, Brian.