Bonnie and Claire

HENRY sits on a couch. CLAIRE sits next to him. Claire’s arms are all over Henry. BONNIE walks in and places a chair to the side of the couch. She then walks to a chair facing the one she set…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Mentally abusing classes in JavaScript

This little post should be fun. We’re going to look at classes, getters, and setters — in plain JavaScript with no libraries.

Style guides? We don’t want any of those. And not from a company that cannot find renters in a housing bubble. If you make a method static, it can’t become a getter or setter.

Aren’t setters made for value validation, hard-coded property observers, and setting multiple properties? That’s the intended purpose. How is a setter function internally different from normal JavaScript functions?

What stands out to you? Have you ever seen someone say, “functions should only ever take one parameter?” Maybe they would like to write all their functions as setters. No evidence points to people programming in setters, but we’ll come back to this idea.

How do getters hold up? We make getters for computed property values, whether combining values (first + last names = full name), or calculating values (Current year — DoB = age). Getters, like setters, invoke a function in an atypical way. This kind of invocation prevents the function from being cloned or inspected. What does this mean for getter functions?

Do any of these make getters frightening? It may appear to quack like a duck, but the getter is a hunter.

What can we do with these tools? Destroy all assumptions programmers make in JavaScript. Let’s abuse both the idea of good coding and all those unfortunate enough to code good.

For setters, this is simple enough. We create setter functions that treat the “new value” as a parameter. A setter that does not set any properties. Is this what is meant by Inversion of Control (it’s not)? Next, let’s destroy the getter. Getters are just class methods (which should only get properties), so we get no properties but start setting properties randomly. Are getters worse than setters since they take no parameters? No. Getters can return! What should we return if we don’t want to get anything? Consider what happens if we return this.

Below is an example of many of these principles put into practice. You’ll see setters that do not set any object properties, getters which are chainable, and getters that modify state.

Add a comment

Related posts:

Why Robots Can Never Be Happy

Emotions are a funny thing. We all experience and understand them. In the future, we want our robots to think and feel like us. But this can never be the case. Let's talk about why.

The Web

I am a poet with a particular interest in Human Rights and current events. This poem speaks of the web of deceit which we can find ourselves in and how our perceptions shape our reality. This is…

How to travel across America on the cheap.

One of the most interesting ways to see America is a Road Trip. I have travelled many times in the United States by road, train and air and the first mode is by far the best for sightseeing and is…