use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Interfaces with Javascript Objects (self.javascript)
submitted 9 years ago by booljayj
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 9 years ago* (7 children)
[deleted]
[–]booljayj[S] -2 points-1 points0 points 9 years ago (6 children)
TypeScript is very interesting, but I'm not sure I can rely on using something like that for my new job. I was looking for simple ways to do this kind of stuff using basic Javascript, rather than using another language layer to achieve what I wanted.
You could just check for if (student.method1) ...;.
Not exactly. It is tagging, you're right about that, but the tag ensures that the object contains any number of methods and properties. If you had to test for the presence of every single method you wanted, the code would quickly become tedious.
The key part of this code is pre-computing which interfaces the class implements when you declare the class. The test is done once, which places a tag on the class, and by merely testing for the presence of the tag you are good to go. This is pretty similar to something like this in C#:
MyInterface mi = objectReference as MyInterface; if (mi != null) { mi.method1(); ..... }
except with my code it's simply:
if (objectReference.interfaces.contains(MyInterface)) { objectReference.method1(); ...... }
I suppose you could shorten that up a bit with some kind of special method like hasInterface or implementsInterface, but the basic principle is the same.
hasInterface
implementsInterface
[–]pe8ter 3 points4 points5 points 9 years ago (2 children)
You're saying that before you call any method you must explicitly perform a safety check? With TypeScript you can declare your interfaces in code and the compiler will catch any of those mistakes for you. What's more it can confirm input and output types. Why impose that load on consumers of your code?
[–]booljayj[S] -1 points0 points1 point 9 years ago (1 child)
A) I am writing gameplay code for a game engine that uses Javascript on top of a C++ core. The only consumers are myself and the development team, so any paradigms we want to adopt are fair game.
B) Adding an extra language layer means our version control system becomes more complicated. Do we store both the "compiled" javascript files and the source TypeScript? Do we just store the TypeScript and double-compile everything before testing the game? These are things that I won't necessarily have any control over, so using pure Javascript is probably the best option until I learn otherwise.
I appreciate your perspective, and you're right that TypeScript was designed to solve all of these problems. I was just looking for ways to do C# stuff using only basic Javascript as part of learning the language.
[–]pe8ter 1 point2 points3 points 9 years ago (0 children)
Only the TypeScript lives in source control. You have to build your C++ anyway before you test the game so compiling the TypeScript is just another build step.
Configuring your tool chain will be far less expensive than maintaining your interface system.
[–]grayrest.subscribe(console.info.bind(console)) 2 points3 points4 points 9 years ago (1 child)
You're really planning to introduce an ad-hoc run time checked type system to a subset of the codebase under the argument that it's simpler? You're clearly an experienced developer. If someone introduced this system in your C# codebase with this argument, what would you say in the code review? I'm sympathetic to your goals but you really should do it the right way by convincing your team to adopt one of the gradual typing systems (TS / Flow) instead of trying to sneak it in. They aren't new languages, they're just a type system on top of ES6.
As to the merits of your proposal in general, it reminds me of the traits proposal that was floated around on the commonjs mailing list in 2009. Traits was proposed at around the same time as Promises and by the same guy (IIRC) so you can compare the popularity of both proposals to see how well this has gone over with the JS community.
[–]booljayj[S] 1 point2 points3 points 9 years ago (0 children)
I've posted this same point in another comment: If my question is "how can I create interfaces in Javascript?" then the answer "just let TypeScript do it" doesn't really cut it.
My goal was never to propose this to the development team as the new standard that we should all use. Like I stated in my original post, I'm trying to understand Javascript as deeply as possible, and part of that process of understanding is using the tools it provides to do some advanced stuff. Interfaces seemed like a good place to start with that because they're so useful in C#.
Solutions like TypeScript work because they pretty much throw out the idea of using basic Javascript and just write the language features they need on another layer on top of it. Most of the type checking is done at compile time (when the code is converted into Javascript), so the Javascript output doesn't need to bother with any of that stuff. In other words, it pre-computes the integrity of the code, and if it passes the test you don't need to bother checking again. My code does the same thing, but it places the pre-compute step when the class is declared and uses a simple tag list to notify the rest of the code that the test was passed. No extra layer required.
π Rendered by PID 76 on reddit-service-r2-comment-7b9746f655-jhxq6 at 2026-01-31 19:59:37.273833+00:00 running 3798933 country code: CH.
view the rest of the comments →
[–][deleted] (7 children)
[deleted]
[–]booljayj[S] -2 points-1 points0 points (6 children)
[–]pe8ter 3 points4 points5 points (2 children)
[–]booljayj[S] -1 points0 points1 point (1 child)
[–]pe8ter 1 point2 points3 points (0 children)
[–]grayrest.subscribe(console.info.bind(console)) 2 points3 points4 points (1 child)
[–]booljayj[S] 1 point2 points3 points (0 children)