Warning! This post was manufactured in a facility that also processes Monoids and may contain traces of functional programming.

I’d like to share with you what is easily one of my top 5 favorite discoveries in programing during the last year: The append operation for javascript Objects. Before you go looking in the API docs: no, this is not part of the official ECMA Script specification. It’s a pattern that I found on the internet and implemented in funcadelic.js . You can use my implementation, or you can write your own, but either way, once you experience how much better append tastes than its analogues in vanilla JavaScript, you’ll have a really hard time going back.

The TL;DR is that you can use append anywhere that you would use Object.assign or the object spread operator except it’s more awesome. How? Well I’m glad you asked.

append is immutable.

It never changes any of its arguments, only ever creates new objects that are derived from the objects that you give it.

let ingredients = append({ eggs: 2}, { mushrooms: 10 }) //=> { eggs: 2, mushrooms: 10 }

I’ll not try to convince you of the benefits of immutability here, only say that as programmers, we truck in that most precious of resources: information . Why willingly destroy it unless absolutely necessary?

When you use append instead of Object.assign , you’ll never have to pay the overhead of that decision, or worse, accidentally mutate some object you never intended to.

append preserves type.

Every JavaScript object has a runtime type. This lets us define custom properties and operations on it. Unfortunately, if we’re making an immutable transformation using Object.assign or Object spread, that type information is erased and no matter what we started out with we revert to a plain old JavaScript object.

class Person { constructor(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } get fullName() { return `${firstName} ${lastName}`; } } let misspelled = new Person("Tony", "Stork"); let corrected = {...misspelled, { lastName: "Stark" } } //=> Object { firstName: "Tony", lastName: "Stark" } corrected.fullName //=> undefined

Notice how the fact that we’re work working with a Person object is just thrown into the dumpster? By contrast, append always preserves type of object that we were working with before.

let misspelled = new Person("Tony", "Stork"); let corrected = append(misspelled, { lastName: "Stark" }); //=> Person { firstName: "Tony", lastName: "Stark" } corrected.fullName //=> "Tony Stark"

I mean you could throw away the type by default, but why?

append is lazy

JavaScript has a really nice feature called getters or “computed properties” that lets you defer evaluating a property’s value until it’s actually needed. This can be a great way to avoid computing a property if the dependencies required won’t be available until later, or you’d just rather not pay the cost of evaluation until you absolutely need to.

Unfortunately, whatever your reasons, both Object.assign and Object spread will interfere with them because they eagerly evaluate each property of the objects being copied over into the result. For example, let’s look at these two objects that have computed properties:

let left = { get thousand() { console.log('calculating 1000 in a really expensive way'); let i = 0; for (; i < 1000; i++); return i; } } let right = { get hundred() { console.log('calculating 100 in a really expensive way'); let i = 0; for (; i < 100; i++); return i; } }

Let’s see what happens when we combine them with Object spread:

let both = {...left, ...right}; // [LOG] calculating 1000 in a really expensive way // [LOG] calculating 100 in a really expensive way

Or how about Object.assign ?

let both = Object.assign({}, left, right)); // [LOG] calculating 1000 in a really expensive way // [LOG] calculating 100 in a really expensive way

But we made those properties lazy for a reason! Just because I’m deriving a new object based off of those properties, doesn’t mean I should have to evaluate them! Well with append you don’t. It will follow the guidelines laid down by each object’s original creator and keep normal properties normal, and computed properties computed.

let both = append(left, right); // nothing printed, because nothing evaluated! // only when we access the properties does the computation run. both.thousand //=> 1000 // [LOG] calculating 1000 in a really expensive way both.hundred //=> 100 // [LOG] calculatingc 100 in a really expensive way append is a universal interface.

Remember when I said that I found append on the internet? That was true, but it also wasn’t the full story. What I actually found on the internet was something far more powerful: the concept of a semigroup . A semigroup is (roughly) a type; any two of whose values can be combined with each other to produce a new value of the same type. For example, JavaScript Array is a semigroup. I can combine or “append” any two Array objects together to produce a single new Array :

[1,2] + [3, 4] //=> [1, 2, 3, 4]

JavaScript String is a semigroup too!

'Hello' + ' World!' //=> Hello World

This got me to thinking: what would a semigroup for object look like? What would it mean to take two Object s and combine them to produce another Object , and the append() function that I’ve been talking about was the inevitable consequence:

Immutable because this is functional programming, and that’s really just a given. Type-preserving : because the append operation of Semigroup must return the same type as its arguments. Lazy : This isn’t strictly a requriment, but as long as we’re building an amped up version of Object.assign we might as well respect object property semantics, no?

This is all to say that using append normalizes this operation to a shared understanding of what a Semigroup is. And in fact, in funcadelic.js, the append operation will work on all these types.

append([1,2], [3, 4]) //=> [1, 2, 3, 4] append('Hello', ' World!') //=> 'Hello World!' append({ eggs: 2 }, { bacon: 2 }); //=> { eggs: 2, bacon: 2 }

One concept, one function, lots and lots of different scenarios. And that’s why I started using append and haven’t looked back since.

I'm Charles, a developer at Frontside. We offer development, training and support services that will fast-track your foundations in practical functional programming for JavaScript.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

代码区博客精选文章
分页:12
转载请注明
本文标题:Four reasons to use append() instead of Object.assign() and Object spread
本站链接:https://www.codesec.net/view/611089.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(77)