Web development is so interesting! But JavaScript is... daunting.
Everything else in Web development is a piece of cake for you, but when you delve deeper into JavaScript, you can’t help but have a tragic feeling of "everyone is awake but you are drunk alone" - just like other Everyone knows some big basic knowledge content that you don’t know, and these content can help you understand all knowledge.
The truth is, it is true, you are missing some important pieces to solve the problem.
And, front-end development has actually gone crazy.
Pull up a chair and sit down. It’s time to write a JavaScript application.
The first step is to prepare the local development environment and run it. So Gulp or Grunt, wait, no... NPM scripts too!
Use WebPACK or Browserify? Require.js? Upgrading to ES6? Or adding Babel to your preprocessing too much?
BDD or regular unit testing? What assertion framework should be used? Of course running the tests from the command line would be nice, so maybe PhantomJS would work too?
Angular or React? Ember? Backbone?
You read some React documentation, "Redux is a predictable state container for JavaScript apps." Awesome! You definitely need one of those.
Let me help you understand why I am saying all of this is so crazy. Let's start with an example and then move to pretty pictures.
This is React's "Hello, world!" application.
// main.js var React = require('react'); var ReactDOM = require('react-dom'); ReactDOM.render( <h1>Hello, world!</h1>, document.getElementById('example') );
Not completely finished.
$ npm install --save react react-dom babelify babel-preset-react $ browserify -t [ babelify --presets [ react ] ] main.js -o bundle.js
There are actually a few steps missing here, such as installing browserify and what you actually need to do to get it to run on the web once you've done that, because that doesn't actually produce a content of the web page. ¯\ _(ツ)_ /¯
After completing this, you will finally need a file called bundle.js, which contains the new React Hello World application — —The program has 19,374 lines of code. And you only need to install browserify, babelify and react-dom, and consider thousands of unknown lines of code.
Marc is almost ready to implement his "Hello World" React app pic.twitter.com/ptdg4yteF1
—— Thomas Fuchs (@thomasfuchs) March 12, 2016
Let’s write a Hello World app using plain JavaScript code.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <title>Hello World</title> </head> <body> <p id="container"></p> <script> document.body.onload = function(){ var container = document.getElementById("container"); container.innerHTML = '<h1>"Hello, world!"</h1>'; } </script> </body> </html>
That’s it. 18 lines of code. You can copy/paste into the index.html file, double-click and load it into your browser. Finish.
If at this point you’re thinking, “Wait a minute, React can do more than this little thing you just wrote, and you can’t write a JavaScript app that way!” You are (big Most of the time) true, but you still have to take a small step to understand why everything is crazy.
The following is the picture I promised.
The vast majority of JavaScript web applications you will work on will fall somewhere in the middle of the bell curve. And in the middle, if you start with a full React stack, you're massively over-engineering your application from the get-go.
That's why everything goes crazy. Most of these tools you think are necessary to solve problems, but you have never encountered such a problem, and you will never encounter such a problem in the future.
Same picture:
Because by default, everyone over-designs their apps, but realizing this, Javascript development The state has become too verbose.
How should you start a JavaScript application? Should I use some tool like React or Angular? Should I use a package manager? If you don't, what should you do? Is testing necessary? Should the markup be generated in Javascript? All of these are questions you should ask yourself before launching a massive technology stack that comes by default.
When you launch a JavaScript application, the key is to pick a point on the bell curve that is just ahead of the level of complexity you think the app will eventually reach.
I'm not going to lie, it takes experience to verify this. But there's a pretty big sweet spot here that lets you launch most JavaScript apps: jQuery plus client-side templates, and super-easy build tools for concatenating and minifying product files (assuming your backend architecture doesn't already have that) if done).
If you know how to build a Javascript app properly, then you will start to understand how, when and why to use a framework or npm/requir/webPack or ES6, when to write tests, and when you should bother making your tests local Running vs. running in the browser, etc., all these questions come up.
Interested in filling those gaps with your JavaScript development knowledge? Want to avoid feeling overwhelmed and massively over-engineering your JavaScript applications in the process? That’s what I’ll be focusing on later, so stay tuned!
The above is the detailed content of Why JavaScript Development Is Such Insanely Detailed Code. For more information, please follow other related articles on the PHP Chinese website!