JavaScript Web is The Future

rubyu2 · December 20, 2015 · Last by rubyu2 replied at December 21, 2015 · 4188 hits

最近两年移动端的 Native 一直比较火,网上也有不少关于 Web 和 Native 的争论。看我标题,就知道其实我是来站队的。

Native 两个致命的缺陷

  • 版本升级不便
  • 开发成本高,兼容 IOS/Android 多版本

Web 的劣势

  • 慢?

很多人都是认为 Web 慢,但是这个问题如何解决呢?

http://www.pocketjavascript.com/blog/2015/11/23/introducing-pokedex-org

Ask any iOS or Android developer how we make our apps so fast, and most likely you'll hear about two major strategies:

  1. Eliminate network calls. Chatty network activity can kill the performance of a mobile app, even on a good 3G or 4G connection. Staring at a loading spinner is not a good user experience.
  2. Use background threads. To hit a silky-smooth 60 FPS, your operations on the main thread must take less than 16ms. Anything unrelated to the UI should be offloaded to a background thread.

其实 Web 已经有现成的方案了:

I believe the web is as capable of solving these problems as native apps, but most web developers just aren't aware that the tools are out there. For the network and concurrency problems, the web has two very good answers:

  1. Offline-first (e.g. IndexedDB and ServiceWorkers)
    1. Web workers

Offline-first 首先就需要一个 ClientDb。

For this app, I decided to use PouchDB for the Pokémon data (because it's good at sync), as well as LocalForage for app state data (because it has a nice key-value API). Both PouchDB and LocalForage are using IndexedDB inside a web worker, which means any database operations are fully non-blocking.

这里有一篇很有意思得文章:http://nolanlawson.com/2015/09/29/indexeddb-websql-localstorage-what-blocks-the-dom/

However, it's also true that the Pokémon data isn't immediately available when the site is first loaded, because it takes awhile to sync from the server. So I'm also using a fallback strategy of "local first, then remote":

Background threads 就必须要用到 Web Worker 了。

简直完美,再也不用阻塞 UI 线程了。

地址:https://github.com/nolanlawson/pokedex.org,这才是未来。

顺便聊下其他的:

  1. Meteor 虽然我不是很喜欢这种 JS 做全栈的方式,因为我一直认为 JS 的单线程方式非常不适合后端开发,但是这个框架的 DDP 协议还是很不错的一个东西,值得借鉴的地方很多。

  2. WebWorker 其实这东西非常有用。https://gist.github.com/nolanlawson/d9e66349635452a95bb1

  3. indexedDB 要想做离线应用,只能指望他了

  4. ES7 Object.observe,虽然我更喜欢 clojurescript 的 immutable 的方式,但是看来不可能实现了。

  5. WebAssembly 未来存在无数的可能

  6. React https://github.com/nsisodiya/flux-inside-web-workers

赞同!我很早之前就这样觉得啦,我觉得手机的发展也会跟电脑端一样,由 native 走向 web cloud,甚至所有的以后再出现的移动端,如智能手表,智能眼镜。。。全部都只是数据收集和数据展示的功能。。。sensor+screen!!电脑和手机只不过是两个最先发展起来的代表而已,或者说是因为某些技术不成熟而妥协产生的过渡产品!

其实不需要找理由,web 和原生的体验完全是两回事。我们同款 app, ios 的做原生,android 的以 ionic 为基础,体验完全不一样。

localForage, webWorker 这些都不是 killer, 我们都用上了,当然有效果,但谈不上靠这个媲美原生的。Object.observe 更谈不上了,只是一个新 API 而已,ES5 靠 polyfill 也能实现。Angular 的内在实现本来就和这个差不多,React 更是超越其上。

而且 Javascript 的开发也不能说是更容易,你要是照着框架来做千篇一律的东西当然不难,但要想玩出花,不费硬功夫是不行的。

我觉得不需要找理由,不需要偷懒。你要是公司,如果有人有预算有追求,肯定上原生。如果是开发者,要勇敢跳出自己的 comfortable zone, 什么好就学什么,用什么。

#2 楼 @billy 是的,原生的优势就在于体验和有硬件支持性。但是用 Web 同样可以做出体验和交互极佳的 App,这样的例子非常多的。我想表达的也是,Web 慢这个问题实际可以完全解决。Native 的优势最终可能只会剩下一个问题,就是硬件优势。而这个优势我觉得将来也可以解决,这只是一个过渡期。

这也不是 comfortable zone 的问题,这些只是我的一些个人见解,肯定有人会同意,会有人反对。

WEB 需要一场变革!

不得不说本文对我的启发挺大!

Web 的本质是一种无缝的发布方式,如果 Native 应用可以做到在发现 bug 或者上新功能时,没有感觉的直接推给用户,一天做 20 次以上,我觉得 Web 的优势就不大了。

Everything could be future, future is a circle.

#6 楼 @ericguo Web 的本质,无缝和跨平台。Native 的本质就是可以方便调用系统资源的调用。Web 的 UI 体验度和速度其实都不是问题。Native 做到无缝和跨平台基本不可能。

我觉得 Web 会是未来的方向。但是何必把 Web 和 Native 的界限划得那么明确。未来两者的界限会越来越模糊的,Web 应该是一种更好的发展思路,而不是仅仅是跑在浏览器里的一堆技术。

#9 楼 @darkbaby123 深得我心 :plus1:

You need to Sign in before reply, if you don't have an account, please Sign up first.