Người vận hành lan truyền
Toán tử lan truyền JavaScript [
const myVehicle = {
brand: 'Ford',
model: 'Mustang',
color: 'red'
}
const updateMyVehicle = {
type: 'car',
year: 2021,
color: 'yellow'
}
const myUpdatedVehicle = {...myVehicle, ...updateMyVehicle}
9] cho phép chúng tôi nhanh chóng sao chép tất cả hoặc một phần của mảng hoặc đối tượng hiện có vào một mảng hoặc đối tượng khác.Thí dụ
const numbersOne = [1, 2, 3];
const numbersTwo = [4, 5, 6];
const numbersCombined = [...numbersOne, ...numbersTwo];
Hãy tự mình thử »
Các toán tử lây lan thường được sử dụng kết hợp với phá hủy.
Thí dụ
Hãy tự mình thử »
const numbers = [1, 2, 3, 4, 5, 6];
const [one, two, ...rest] = numbers;
Hãy tự mình thử »
Các toán tử lây lan thường được sử dụng kết hợp với phá hủy.
Thí dụ
Hãy tự mình thử »
const myVehicle = {
brand: 'Ford',
model: 'Mustang',
color: 'red'
}
const updateMyVehicle = {
type: 'car',
year: 2021,
color: 'yellow'
}
const myUpdatedVehicle = {...myVehicle, ...updateMyVehicle}
Hãy tự mình thử »
Các toán tử lây lan thường được sử dụng kết hợp với phá hủy.
Gán các mục thứ nhất và thứ hai từ const arrayOne = ['a', 'b', 'c'];
const arrayTwo = [1, 2, 3];
const arraysCombined = [];
0 cho các biến và đặt phần còn lại vào một mảng:
Exercise:
Chúng ta cũng có thể sử dụng toán tử lây lan với các đối tượng:
const arrayOne = ['a', 'b', 'c']; const arrayTwo = [1, 2, 3]; const arraysCombined = [];
Kết hợp hai đối tượng sau:
[+:
React is a JavaScript library for building user interfaces.
React is used to build single-page applications.
React allows us to create reusable UI components.
Start learning React now ❯
Learning by Examples
Our "Show React" tool makes it easy to demonstrate React. It shows both the code and the result.
Example:
import React from 'react';
import ReactDOM from 'react-dom/client';
function Hello[props] {
return Hello World!
;
}
const root = ReactDOM.createRoot[document.getElementById["root"]];
root.render[];
Run Example »
Learning by Exercises
React Exercises
Exercise:
Enter the correct ReactDOM method to render the React element to the DOM.
ReactDOM.[myElement, document.getElementById['root']];
Start the Exercise
React Quiz
Test your React skills with a quiz.
React Quiz
Test your React skills with a quiz.
My Learning
Track your progress with the free "My Learning" program here at W3Schools.
Log into your account, and start earning points!
This is an optional feature. You can study W3Schools without using My Learning.
Create React App
To learn and test React, you should set up a React Environment on your computer.
This tutorial uses the
const arrayOne = ['a', 'b', 'c']; const arrayTwo = [1, 2, 3]; const arraysCombined = [];3.
The
const arrayOne = ['a', 'b', 'c']; const arrayTwo = [1, 2, 3]; const arraysCombined = [];3 tool is an officially supported way to create React applications.
Node.js is required to use
const arrayOne = ['a', 'b', 'c']; const arrayTwo = [1, 2, 3]; const arraysCombined = [];3.
Open your terminal in the directory you would like to create your application.
Run this command to create a React application named
const arrayOne = ['a', 'b', 'c']; const arrayTwo = [1, 2, 3]; const arraysCombined = [];6:
npx create-react-app my-react-app
const arrayOne = ['a', 'b', 'c'];
const arrayTwo = [1, 2, 3];
const arraysCombined = [];
3 will set up everything you need to run a React application. If you've previously installed
const arrayOne = ['a', 'b', 'c']; const arrayTwo = [1, 2, 3]; const arraysCombined = [];3 globally, it is recommended that you uninstall the package to ensure npx always uses the latest version of
const arrayOne = ['a', 'b', 'c']; const arrayTwo = [1, 2, 3]; const arraysCombined = [];3. To uninstall, run this command:
import React from 'react';
import ReactDOM from 'react-dom/client';
function Hello[props] {
return Hello World!
;
}
const root = ReactDOM.createRoot[document.getElementById["root"]];
root.render[];
0. Note: If you've previously installed const arrayOne = ['a', 'b', 'c'];
const arrayTwo = [1, 2, 3];
const arraysCombined = [];
3 globally, it is recommended that you uninstall the package to ensure npx always uses the latest version of const arrayOne = ['a', 'b', 'c'];
const arrayTwo = [1, 2, 3];
const arraysCombined = [];
3. To uninstall, run this command: import React from 'react';
import ReactDOM from 'react-dom/client';
function Hello[props] {
return Hello World!
;
}
const root = ReactDOM.createRoot[document.getElementById["root"]];
root.render[];
0.
import React from 'react';
import ReactDOM from 'react-dom/client';
function Hello[props] {
return Hello World!
;
}
const root = ReactDOM.createRoot[document.getElementById["root"]];
root.render[];
Run the React Application
Run this command to move to the
const arrayOne = ['a', 'b', 'c']; const arrayTwo = [1, 2, 3]; const arraysCombined = [];6 directory:
Run this command to execute the React application
import React from 'react';
import ReactDOM from 'react-dom/client';
function Hello[props] {
return Hello World!
;
}
const root = ReactDOM.createRoot[document.getElementById["root"]];
root.render[];
2:A new browser window will pop up with your newly created React App! If not, open your browser and type
import React from 'react';
import ReactDOM from 'react-dom/client';
function Hello[props] {
return Hello World!
;
}
const root = ReactDOM.createRoot[document.getElementById["root"]];
root.render[];
3 in the address bar.The result:
You will learn more about the const arrayOne = ['a', 'b', 'c'];
const arrayTwo = [1, 2, 3];
const arraysCombined = [];
3 in the React Get Started chapter.
What You Should Already Know
- Before starting with React.JS, you should have intermediate experience in:
- HTML
- CSS
JavaScript
You should also have some experience with the new JavaScript features introduced in ECMAScript 6 [ES6], you will learn about them in the React ES6 chapter.
Kickstart your careerthe course
Get certified by completing the course
Get certifiedChào các bạn, để tiếp tục chuỗi bài về Javascript của mình, hôm nay mình sẽ viết về một operator rất hữu ích, đó là spread operator [được viết là
Math.max[...[1,3,5]] // output: 5
0]. Spread operator là một cách rất hữu dụng và ngắn gọn để dùng trong các thao tác với mảng như thêm phần tử vào mảng, kết hợp mảng [hoặc object], truyền tham số mảng vào function, ... Chúng ta sẽ cùng tìm hiểu chi tiết nhé.Nội dung chính ShowShow
- Spread operator là gì ?
- Vậy thì Math.max[...[1,3,5]] // output: 5 0 dùng để làm gì ?
- Những ví dụ khác về spread operator Math.max[...[1,3,5]] // output: 5 0
- Sao chép mảng
- Sử dụng mảng như danh sách các tham số
- Thêm phần tử vào mảng
- Thao tác với state trong React
- Chuyển NodeList thành một array
- Sau đây mình sẽ giới thiệu tới các bạn một vài ví dụ mà spread operator có thể làm được như sao chép mảng, tách string thành các characters, hoặc là kết hợp các thuộc tính của một object
- Kết luận
Spread operator là gì ?
Vậy thì Math.max[...[1,3,5]] // output: 5 0 dùng để làm gì ?
Những ví dụ khác về spread operator Math.max[...[1,3,5]] // output: 5 0Sao chép mảng
Sử dụng mảng như danh sách các tham số
Math.max[...[1,3,5]] // output: 5
0.Vậy thì Math.max[...[1,3,5]] // output: 5 0 dùng để làm gì ?
Những ví dụ khác về spread operator Math.max[...[1,3,5]] // output: 5 0
Sao chép mảng
Math.max[1,3,5] // output: 5
Math.max[[1,3,5]] // output: NaN
Sử dụng mảng như danh sách các tham số
Thêm phần tử vào mảngThao tác với state trong React
Chuyển NodeList thành một array
- Sau đây mình sẽ giới thiệu tới các bạn một vài ví dụ mà spread operator có thể làm được như sao chép mảng, tách string thành các characters, hoặc là kết hợp các thuộc tính của một object
- Kết luận
- Trong Javascript, spread operator là nói đến cách sử dụng ký hiệu dấu ba chấm
0. Theo Javascrip.info thì spread operator được định nghĩa như sau :Math.max[...[1,3,5]] // output: 5
- Thao tác với state trong React
- Chuyển NodeList thành một array
- Chuyển NodeList thành một array
Những ví dụ khác về spread operator Math.max[...[1,3,5]] // output: 5 0
Sau đây mình sẽ giới thiệu tới các bạn một vài ví dụ mà spread operator có thể làm được như sao chép mảng, tách string thành các characters, hoặc là kết hợp các thuộc tính của một object
const numbers = [1, 2, 3, 4, 5, 6];
const [one, two, ...rest] = numbers;
3Sao chép mảng
Sử dụng mảng như danh sách các tham số
Thêm phần tử vào mảngSử dụng mảng như danh sách các tham số
Thêm phần tử vào mảng
Thao tác với state trong React
Chuyển NodeList thành một arraySau đây mình sẽ giới thiệu tới các bạn một vài ví dụ mà spread operator có thể làm được như sao chép mảng, tách string thành các characters, hoặc là kết hợp các thuộc tính của một object
Kết luậnThêm phần tử vào mảng
Thao tác với state trong React
const myVehicle = {
brand: 'Ford',
model: 'Mustang',
color: 'red'
}
const updateMyVehicle = {
type: 'car',
year: 2021,
color: 'yellow'
}
const myUpdatedVehicle = {...myVehicle, ...updateMyVehicle}
2Thao tác với state trong React
Kết hợp các objects
const myVehicle = {
brand: 'Ford',
model: 'Mustang',
color: 'red'
}
const updateMyVehicle = {
type: 'car',
year: 2021,
color: 'yellow'
}
const myUpdatedVehicle = {...myVehicle, ...updateMyVehicle}
3Chuyển NodeList thành một array
Những ví dụ khác về spread operator
Math.max[...[1,3,5]] // output: 5
0const myVehicle = {
brand: 'Ford',
model: 'Mustang',
color: 'red'
}
const updateMyVehicle = {
type: 'car',
year: 2021,
color: 'yellow'
}
const myUpdatedVehicle = {...myVehicle, ...updateMyVehicle}
5Sau đây mình sẽ giới thiệu tới các bạn một vài ví dụ mà spread operator có thể làm được như sao chép mảng, tách string thành các characters, hoặc là kết hợp các thuộc tính của một object
Sao chép mảng
const myVehicle = {
brand: 'Ford',
model: 'Mustang',
color: 'red'
}
const updateMyVehicle = {
type: 'car',
year: 2021,
color: 'yellow'
}
const myUpdatedVehicle = {...myVehicle, ...updateMyVehicle}
6 Với spread operator
Math.max[...[1,3,5]] // output: 5
0, chúng ta có thể sao chép mảng một cách rất ngắn gọn, bên cạnh đó việc thêm một hay nhiều phần tử vào mảng cũng rất dễ dàng :Kết luận
Kể từ khi ra đời từ phiên bản ES6 [ES2015], spread operator
Math.max[...[1,3,5]] // output: 5
0 đã được cộng đồng lập trình viên Javascript rất yêu thích vì tính hữu dụng và ngắn gọn của nó khi thao tác với mảng và object. Bản thân mình cũng thường xuyên sử dụng nó khi làm việc với React Hooks, nhất là khi thêm một phần tử vào mảng React state.Việc biết thêm những cú pháp mới sẽ giúp chúng ta tiết kiệm thời gian khi code, đồng thời giúp code của chúng ta trở nên dễ đọc hơn rất nhiều. Và mình hi vọng bài chia sẻ này của mình sẽ giúp ích cho các bạn trong "sự nghiệp" code đầy gian nan và vất vả của bản thân nhé
Tài liệu tham khảo: //www.geeksforgeeks.org/javascript-rest-operator/ //medium.com/coding-at-dawn/how-to-use-the-spread-operator-in-javascript-b9e4a8b06fab