简体   繁体   中英

javascript fluent api vs options object

A javascripter I respect talks about how you should avoid large options objects in favor of a fluent api.

Why? What are the pros and cons?

Avoid large options objects

If you component truly only takes a few options, and are unlikely to change after the fact, then an options object may be suitable. I strongly suggest considering a fluent API, even if you provide an options object. This makes code considerably cleaner, as the fluent API can back each key in the options object, which otherwise would promote extremely large plugins. Remember build up to a user-friendly api, do not start there.

https://github.com/component/component/wiki/Building-better-components

I feel like this is an "opinion question" with out a definite answer so here's my opinion.

It makes development easier. Modern Javascript IDEs can inspect Javascript sources and give code completion hints. Visual Studio, Aptana and WebStorm to name a few. If you have a fluent API, the IDE can list all the options methods which will allow you to enter code faster and it will also be able to show you the documentation for each option. If you just pass an options object, you have to know what the shape of that object should be and you also have to know what options you want to use and what they do since the IDE wouldn't be able to tell you that.

Matthew

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM