Translated from developer.mozilla.org/en-US/docs/…
Each Javascript framework has its own different way of updating the DOM, handling browser events, and providing a pleasant experience for developers to use. This article will explore the main features of the “Big Four” frameworks, taking a high-level look at how they work and how they differ.
Domain-specific Language (DSL)
All of the frameworks discussed in this article are javascript-driven and allow you to build your own applications using DSLS. In particular, React uses JSX on a large scale to write its own components, and Ember uses Handlebars. Unlike HTML, these languages know how to read data values, which can be used to simplify the process of writing a UI.
Angular applications often use Typescript heavily. Typesscrit does not involve UI writing, but it is a DSL language with some notable differences from Javascript.
DSLS cannot be run directly by browsers; They must first be converted to Javascript or HTML. Transformation is an additional step in the development process, but framework templates generally contain the necessary tools to handle this step, or can be configured to include it. You don’t need to use these DSL languages when building framework applications, but embracing them will simplify your development process and make it easier to reach out to the framework community.
JSX
JSX, which supports Javascript and XML, is an extension of the Javascript language that brings htML-like syntax to the Javascript environment. It was developed by the React team for use in React, but can also be used to develop other applications – such as Vue.
Here is a simple JSX example:
const subject = "World"; const header = { <header> <h1>Hello, {subject}! </h1> </header> );Copy the code
This expression represents an HTML
element. The curly bracketed subject in line 4 tells the application to read the value of subject and insert it into our
When using React, JSX from the previous section is compiled to look like this:
var subject = "World";
var header = React.createElement("header", null,
React.createElement("h1", null, "Hello, ", subject, "!")
);
Copy the code
When the browser does the final rendering, the above fragments will all produce HTML, like this:
<header> <h1>Hello, World! </h1> </header>Copy the code
Handlebars
The Handlebars template language is not specific to Ember applications, but it is used heavily in Ember applications. Handlebars code is similar to HTML, but it can choose to extract data from elsewhere. This data can be used to influence the HTML that the application eventually builds.
Like JSX, Handlebars uses curly braces to inject the value of a variable. Handlebars use double curly braces instead of single curly braces.
Give me an example of a template
<header> <h1>Hello, {{subject}}! </h1> </header>Copy the code
And this data
{
subject: "World"
}
Copy the code
HandleBars will build pages like this
<header> <h1>Hello, World! </h1> </header>Copy the code
TypeScript
TypeScript is a superset of JavaScript, which means it extends JavaScript — all JavaScript code is valid TypeScript, but not vice versa. TypeScript is useful because it allows developers to enforce their code. For example, consider a function add() that takes the integers A and b and returns their sum.
In Js, this method looks like this
function add(a, b) {
return a + b;
}
Copy the code
This code may seem trivial to someone used to JavaScript, but it can still be clearer. JavaScript allows us to concatenate strings together using the + operator, so if a and B are strings, this function still technically works — it probably won’t give you the result you want. What if we just want numbers passed into this function? TypeScript makes this possible:
function add(a: number, b: number) {
return a + b;
}
Copy the code
The: number after each argument tells TypeScript that both A and B must be numbers. If we use this function and pass ‘2’ as an argument, TypeScript will throw errors at compile time and we will be forced to fix our errors. We could write our own JavaScript to throw these errors for us, but that would make our source code even more verbose. It might make more sense to let TypeScript handle such checks for us. (Note: In fact, since I use TS to now, ts type check is not as simple as the example, it is really difficult to understand the real use of TS, I think the friends who have used TS to develop large projects should have experienced it).
Writing Components
As described in the previous chapter, most frameworks have some kind of component model. React components can be written using JSX, Ember components using Handlebars, and Angular and Vue components can easily extend HTML using template syntax. Regardless of their views on how to write components, each framework’s components provide a way to describe the external properties they might need, the internal states that components should manage, and the events that users can trigger on component tags. The code snippets for the rest of this section use React as an example and are written in JSX.
The Properties (attributes)
The React representation of the AuthorCredit component might look like this:
function AuthorCredit(props) {
return (
<figure>
<img src={props.src} alt={props.alt} />
<figcaption>{props.byline}</figcaption>
</figure>
);
}
Copy the code
{props. SRC}, {props. Alt}, and {props. Byline} represent where our props will be inserted into the component. To render this component, we write code like this where we want to render it (possibly within another component) :
<AuthorCredit
src="./assets/zelda.png"
alt="Portrait of Zelda Schiff"
byline="Zelda Schiff is editor-in-chief of the Library Times."
/>
Copy the code
This will eventually render the following in the browser