avatarEmma Austin

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

8604

Abstract

<span class="hljs-keyword">const</span> boundFn = fn.<span class="hljs-title function_">bind</span>(<span class="hljs-variable language_">this</span>); <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">defineProperty</span>(<span class="hljs-variable language_">this</span>, key, { <span class="hljs-attr">value</span>: boundFn, <span class="hljs-attr">configurable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">writable</span>: <span class="hljs-literal">true</span>, }); <span class="hljs-keyword">return</span> boundFn; }, }; }

<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyComponent</span> { <span class="hljs-title function_">constructor</span>(<span class="hljs-params">props</span>) { <span class="hljs-variable language_">this</span>.<span class="hljs-property">props</span> = props; } @autobind <span class="hljs-title function_">handleClick</span>(<span class="hljs-params"></span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">props</span>); } }</pre></div><p id="0013">In this example, the decorator is used to decorate the function so that it automatically binds this when called and returns a new function. This way, after instantiating , you don’t need to manually bind this when calling the function.<code>autobindhandleClickMyComponentthis.handleClick()</code></p><h1 id="3ffe">Logging</h1><p id="7a28">Decorators can be used to record logs, including printing information such as function calls, function execution times, etc.</p><div id="1a70"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">log</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">Function <span class="hljs-subst">${name}</span> called with <span class="hljs-subst">${args}</span></span>); <span class="hljs-keyword">const</span> start = performance.<span class="hljs-title function_">now</span>(); <span class="hljs-keyword">const</span> result = originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); <span class="hljs-keyword">const</span> duration = performance.<span class="hljs-title function_">now</span>() - start; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">Function <span class="hljs-subst">${name}</span> completed in <span class="hljs-subst">${duration}</span>ms</span>); <span class="hljs-keyword">return</span> result; }; <span class="hljs-keyword">return</span> descriptor } <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyClass</span> { @log <span class="hljs-title function_">myMethod</span>(<span class="hljs-params">arg1, arg2</span>) { <span class="hljs-keyword">return</span> arg1 + arg2; } }

<span class="hljs-keyword">const</span> obj = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyClass</span>(); obj.<span class="hljs-title function_">myMethod</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); <span class="hljs-comment">// Output: </span> <span class="hljs-comment">// Function myMethod called with 1,2</span> <span class="hljs-comment">// Function myMethod completed in 0.013614237010165215ms</span></pre></div><h1 id="aff7">Authentication authentication</h1><p id="bbff">Decorators can also be used to check a user’s authentication status and permissions to prevent unauthorized users from accessing sensitive data or performing actions on a regular basis.</p><div id="be1a"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">authorization</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">...args</span>) { <span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">isAuthenticated</span>()) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Access denied! Not authenticated'</span>); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">if</span> (!<span class="hljs-variable language_">this</span>.<span class="hljs-title function_">hasAccessTo</span>(name)) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">Access denied! User does not have permission to <span class="hljs-subst">${name}</span></span>); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">return</span> originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); }; <span class="hljs-keyword">return</span> descriptor; }

<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyApi</span> { <span class="hljs-title function_">isAuthenticated</span>(<span class="hljs-params"></span>) { <span class="hljs-comment">// perform authentication check</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; } <span class="hljs-title function_">hasAccessTo</span>(<span class="hljs-params">endpoint</span>) { <span class="hljs-comment">// perform authorization check</span> <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; } @authorization <span class="hljs-title function_">getUsers</span>(<span class="hljs-params"></span>) { <span class="hljs-comment">// return users data</span> } @authorization <span class="hljs-title function_">deleteUser</span>(<span class="hljs-params">id</span>) { <span class="hljs-comment">// delete user with id</span> } }</pre></div><h1 id="646e">Cache</h1><p id="3465">Decorators can also be used to cache the execution results of functions to avoid double evaluation.</p><div id="5257"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">memoize</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>; <span class="hljs-keyword">const</span> cache = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Map</span>();

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) { <span class="hljs-keyword">const</span> cacheKey = args.<span class="hljs-title function_">toString</span>(); <span class="hljs-keyword">if</span> (cache.<span class="hljs-title function_">has</span>(cacheKey)) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">cache hit: <span class="hljs-subst">${cacheKey}</span></span>); <span class="hljs-keyword">return</span> cache.<span class="hljs-title function_">get</span>(cacheKey); } <span class="hljs-keyword">const</span> result = originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">cache miss: <span class="hljs-subst">${cacheKey}</span></span>); cache.<span class="hljs-title function_">set</span>(cacheKey, result); <span class="hljs-keyword">return</span> result; }; <span class="hljs-keyword">return</span> descriptor; }

<span class="hljs-keyword">class

Options

</span> <span class="hljs-title class_">MyMath</span> { @memoize <span class="hljs-title function_">calculate</span>(<span class="hljs-params">num</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'calculate called'</span>); <span class="hljs-keyword">return</span> num * <span class="hljs-number">2</span>; } }

<span class="hljs-keyword">const</span> math = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyMath</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(math.<span class="hljs-title function_">calculate</span>(<span class="hljs-number">10</span>)); <span class="hljs-comment">// Output: </span> <span class="hljs-comment">// calculate called</span> <span class="hljs-comment">// cache miss: 10</span> <span class="hljs-comment">// 20</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(math.<span class="hljs-title function_">calculate</span>(<span class="hljs-number">10</span>)); <span class="hljs-comment">// Output: </span> <span class="hljs-comment">// cache hit: 10</span> <span class="hljs-comment">// 20</span></pre></div><h1 id="57d9">Aspect-oriented programming</h1><p id="0d61">Decorators can be used to implement facet-oriented programming, that is, to add functionality at runtime without modifying the original code.</p><div id="bc4d"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">validate</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) { <span class="hljs-keyword">const</span> isValid = args.<span class="hljs-title function_">every</span>(<span class="hljs-function"><span class="hljs-params">arg</span> =></span> <span class="hljs-keyword">typeof</span> arg === <span class="hljs-string">'string'</span> && arg.<span class="hljs-property">length</span> > <span class="hljs-number">0</span>); <span class="hljs-keyword">if</span> (!isValid) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">'Invalid arguments'</span>); <span class="hljs-keyword">return</span>; } <span class="hljs-keyword">return</span> originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); }; <span class="hljs-keyword">return</span> descriptor; }

<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyForm</span> { @validate <span class="hljs-title function_">submit</span>(<span class="hljs-params">name, email, message</span>) { <span class="hljs-comment">// submit the form</span> } }

<span class="hljs-keyword">const</span> form = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyForm</span>(); form.<span class="hljs-title function_">submit</span>(<span class="hljs-string">''</span>, <span class="hljs-string">'[email protected]'</span>, <span class="hljs-string">'Hello world'</span>); <span class="hljs-comment">// Output: Invalid arguments</span></pre></div><h1 id="c8da">Reversible decorators</h1><p id="5322">Decorators can also be applied in reversible scenes, for example, you can add a reversible decorator to modify function behavior.</p><div id="b868"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">reverse</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) { args.<span class="hljs-title function_">reverse</span>(); <span class="hljs-keyword">return</span> originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args); }; <span class="hljs-keyword">return</span> descriptor; } <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyMath</span> { @reverse <span class="hljs-title function_">calculate</span>(<span class="hljs-params">num1, num2</span>) { <span class="hljs-keyword">return</span> num1 + num2; } }

<span class="hljs-keyword">const</span> math = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyMath</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(math.<span class="hljs-title function_">calculate</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)); <span class="hljs-comment">// Output: 3</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(math.<span class="hljs-property">calculate</span>.<span class="hljs-title function_">reversed</span>(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)); <span class="hljs-comment">// Output: 3</span></pre></div><h1 id="2ddc">Automatic type checking</h1><p id="037c">Decorators can be applied to automatic type checking, for example, you can add a decorator to ensure that the type of a function parameter is correct.</p><div id="a63a"><pre><span class="hljs-keyword">function</span> <span class="hljs-title function_">checkType</span>(<span class="hljs-params">expectedType</span>) { <span class="hljs-keyword">return</span> <span class="hljs-keyword">function</span>(<span class="hljs-params">target, name, descriptor</span>) { <span class="hljs-keyword">const</span> originalMethod = descriptor.<span class="hljs-property">value</span>;

descriptor.<span class="hljs-property">value</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) {
      <span class="hljs-keyword">const</span> invalidArgs = args.<span class="hljs-title function_">filter</span>(<span class="hljs-function"><span class="hljs-params">arg</span> =&gt;</span> <span class="hljs-keyword">typeof</span> arg !== expectedType);
      <span class="hljs-keyword">if</span> (invalidArgs.<span class="hljs-property">length</span> &gt; <span class="hljs-number">0</span>) {
        <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`Invalid arguments: <span class="hljs-subst">${invalidArgs}</span>`</span>);
        <span class="hljs-keyword">return</span>;
      }
      <span class="hljs-keyword">return</span> originalMethod.<span class="hljs-title function_">apply</span>(<span class="hljs-variable language_">this</span>, args);
    };
    <span class="hljs-keyword">return</span> descriptor;
  }

}

<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyMath</span> { @<span class="hljs-title function_">checkType</span>(<span class="hljs-string">'number'</span>) <span class="hljs-title function_">add</span>(<span class="hljs-params">num1, num2</span>) { <span class="hljs-keyword">return</span> num1 + num2; } }

<span class="hljs-keyword">const</span> math = <span class="hljs-keyword">new</span> <span class="hljs-title class_">MyMath</span>(); math.<span class="hljs-title function_">add</span>(<span class="hljs-number">1</span>, <span class="hljs-string">'2'</span>); <span class="hljs-comment">// Output: Invalid arguments: 2</span></pre></div><p id="dd86"><i>More content at <a href="https://plainenglish.io/"><b>PlainEnglish.io</b></a>.</i></p><p id="5cf7"><i>Sign up for our <a href="http://newsletter.plainenglish.io/"><b>free weekly newsletter</b></a>. Follow us on <a href="https://twitter.com/inPlainEngHQ"><b>Twitter</b></a></i>, <a href="https://www.linkedin.com/company/inplainenglish/"><b><i>LinkedIn</i></b></a><i>, <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><b>YouTube</b></a>, and <a href="https://discord.gg/GtDtUAvyhW"><b>Discord</b></a><b>.</b></i></p><p id="7057"><b><i>Interested in scaling your software startup</i></b><i>? Check out <a href="https://circuit.ooo?utm=publication-post-cta"><b>Circuit</b></a>.</i></p></article></body>

Why I Write About Sex Explicitly

No, it’s not for attention

Photo by: Dean Drobot / Shutterstock

I wanted to be a writer from a very early age. I felt a compulsion to write about my life and share those stories with others.

The only problem was, my life seemed so dull. I was a tween doing tweeny things, then a teen doing teeny things. Nothing about it felt special.

After taking a long detour through psychology, early childhood education, working as a preschool teacher, and then becoming a stay-at-home mom, I decided to finally give writing a shot.

I focused on erotica. I considered writing about my own sex life, but it still felt too mundane to be interesting. Instead, I invented women and put them in whatever scenarios my horny imagination came up with.

Then, I started mining my own sexual history for ideas. I thought about things I’ve done and places I fucked and the stories based on them came out decent. That’s when I realized that maybe, just maybe, I could branch out into blogging. That maybe my own personal life and those long, heated conversations I have with my husband about every sexual topic there is could be of interest to others.

I also worried that I wouldn’t be a good fit for the blogging world. I love reading about sex, but most of the content I came across was dry and technical.

I decided Medium would be a good platform to get started on because there were already a lot of people writing relatable and vulnerable stuff. But with very few exceptions, a lot of the posts about sex felt more educational and clinical than personal. Did it even make sense for me to write explicit material here?

I considered toning it down. Being dirty served me well when I was writing erotica, but maybe it would get in the way of blogging.

Well, I tried, but it never felt natural. It’s just not the way I talk. I’ve had a sailor’s mouth for as long as I’ve been allowed to get away with it, and reining it in was tough. I found it hard to stay engaged and interested when I was modulating my voice so much.

So, I decided to just let loose, write dirty, and make my posts as x-rated as I feel they should be.

That worked out well for me, probably a lot better than it would have if I tried to fake a clean voice. It made writing fun. I know it alienated some people, but I also managed to find readers who don’t seem to mind my style.

Gradually, I found my voice. The way I write became more and more me. But as my writing style changed and became more authentic, writing explicitly about my sex life is the one thing that stayed constant.

So, I’m just here doing my thing. And I’ve come to discover that some people have strong opinions about that. There’s been a lot of behind-the-scenes discussion among writers and bloggers about women who write about sex.

There are some people who speculate that we write about sex as an easy way to make money. That’s already been debunked here by traceybyfire, here and here by Shannon Ashley, and you just have to scroll through the list of popular posts on Medium to see that writing about sex isn’t the best way to make it rain.

Another common claim is that we write about our sex lives for cheap attention. It’s a way for desperate writers to get easy views and reads.

It never felt that way to me.

I write about sex to make money, but I didn’t choose sex for its money-making potential.

I also don’t write about it to get attention (no more than any other writer wants attention, anyway — we’re all putting our work online for a reason). In fact, I worried that writing about sex would turn people away from my writing. I even briefly wondered if I should write about other subjects because writing dirty sex posts might make them unappealing to most readers.

I know writers are supposed to avoid overdone cliches. But I can’t help but think that I didn’t choose sex — sex chose me. I’ve been obsessed with it from a very young age and I don’t know why — it’s just a topic I always felt drawn to.

I knew plenty of people who wanted to have sex, but none of them seemed as interested in it as I was. It’s the one thing I never get tired of exploring, researching, and discussing.

Even before I had sex, I had a highly curious, positive, and accepting attitude toward all things sex.

I’ve always gravitated toward it, and I would have even if it was a bottom-of-the-barrel topic when it came to making money or getting easy reads.

So, if I don’t write about sex because it’s easy money or because I’m desperate for attention, why do I write about it? And why so explicitly?

I Write Adult Stuff for an Adult Audience

I never felt the need to censor or tone down the language in my writing because I’m writing with an adult audience in mind. If I’m writing for adults, why would I shy away from using adult language?

I’ve never used academic language when I’m having an intimate conversation about sex with a friend. I never asked “Have you copulated with your boyfriend yet?” or asked “When you give fellatio, does he reciprocate with cunnilingus?”

No matter who I was talking to, we’d just talk about fucking and pussy eating instead, so that’s the kind of language that ends up in my articles.

It’s also what I like reading. My research takes me to some pretty dry articles sometimes, but I like when sex writing is casual and unrestrained. I like feeling like a writer is baring themselves to me, whether that’s through their stories or just the way they write. It feels intimate that way.

I Want to Take the Taboo Out of Sex

There’s too much shame surrounding sex. It’s still treated like something that belongs behind closed doors and is absolutely nobody’s business. And yeah, there’s no reason to pry into anyone’s life, but we should also feel free to be open about the sex, masturbation, porn, and erotica that is a part of our lives without hesitation.

Being open is liberating, and I want to challenge the assumption that we have to keep all things related to sex completely and utterly private. Using the kinds of frilly euphemisms I used to read in old romance novels won’t push things in that direction.

Even though I was always very into sex, I was very embarrassed by the language surrounding it. I always tried to beat around the bush instead of saying what I wanted to say in the most straightforward way I could. It didn’t do me any favors. It didn’t help me become more comfortable with sex. In fact, it did the opposite. It made it harder to ask questions, to talk, and to communicate about it without feeling some kind of embarrassment about the words that were spilling out of my mouth.

It’s only after I allowed myself to let loose verbally that I could confront and overcome that shame.

I write about some difficult topics like my struggles with low libido, difficulties I’ve had in my marriage, and the importance of good sex and masturbation for mental health. All of this has helped me grow (and hopefully helped out one or two other people along the way), but they’re all conversations I struggled to have before I could speak and write explicitly about them.

I Don’t Want to Treat Sex Too Seriously

Sex is a big deal to me, but I don’t want to take it too seriously. It’s fun (well, it should be anyway) so I try to write about it in a way that’s light and breezy.

I’ve written about really heavy and dark topics, but for the most part I want to convey my enthusiasm for sex. It’s a feel-good topic to me and I want my writing to reflect that.

I also don’t want to be coy about it. I don’t want to treat sexuality like something that’s on display behind glass — I want to write about it from my lived, first-hand experience. And being true to that experience means getting explicit. So, when I write about anal sex, I’m not going abstract — I’m going to talk about what it’s like to get fucked in the ass.

The more I mince words, the more I feel like I’m turning sex into this precious, untouchable thing instead of the delicious, messy, silly thing it is.

I Write the Kinds of Articles I Wish I Could’ve Read as a Teenager

I write for an adult audience, but I’m also writing the articles I wish were there when I really needed them.

When I was first exploring my sexuality, learning about sex, and even having sex, there were very few resources that spoke to me. My sex ed classes were a joke. The kinds of pamphlets you might get from a nurse were boring and didn’t reflect the way any teenager really thought about sex.

Other than my hero Sue Johanson, everything I came across was very sterile and usually very vanilla.

It left me confused. I didn’t have any idea why I liked the things I liked and found appealing or arousing. I didn’t have any vocabulary to work it out.

Until I discovered Reddit and a few other spaces like it on the internet, I didn’t have any resources to help me understand why I was so aroused by the idea of someone coming inside me when I was so careful about not getting pregnant. I loved giving my boyfriend blowjobs, but I couldn’t figure out why I could be so into something that didn’t give me physical stimulation. I didn’t really get any of it until I came across other people spilling their guts online.

Sadly, a lot of it came too late. I had been through plenty of mediocre sex that I didn’t really know how to improve. I was raped and just thought it was some bad sex and that it was my fault. I stayed in an abusive relationship because I thought it was love. I was perplexed about my sexual orientation and scared of the feelings that came with it. It even took me a long-ass time to realize I’m polyamorous even though all the signs were there.

I write about sex to process all those things. But I also write things that would’ve have helped me tremendously throughout my life. And I try to write it in a way that I hope will help people who are going through the tough stuff feel understood.

I might not be the next Sue Johanson (because there will never be another Sue Johanson), but I can at least do my best not to write like a nurse’s pamphlet.

It’s the Only Way I Know

There are a lot of reasons I write about sex and write about it explicitly. But in the end, it’s just what comes natural to me. The more I hone my voice, the more I realize that the dirty, x-rated part is integral to it.

I don’t think everyone needs to write the way I do, and I don’t look down on people who write differently. In fact, some of my favorite writers don’t write explicitly at all. I love promoting sex positivity, and I think shaming people for the way they write about sex seems sex negative to me. There’s room for all of us, and we can all find readers who like our style.

I won’t be everybody’s cup of tea, and that’s okay. There are plenty of writers out there tackling the same subjects in a different mode. But I’ll just be over here in my little corner, writing my explicit stuff and feeling grateful for anyone who wants to join me.

Let’s keep in touch! Sign up for my weekly newsletter (I won’t send you anything without your enthusiastic consent!)

❤ If you liked this post, you might also love:

Sex
Writing
Culture
Feminism
Sexuality
Recommended from ReadMedium