Though I\\'ve changed the value of info, since infoReadOnly is actually a live copy of info my changes are reflected in infoReadOnly as well. Yet you may not change the values using infoReadOnly directly:

const info = ref({  first: \\'Adnan\\',  last: \\'Babakan\\'})const infoReadOnly = readonly(info)onMounted(() => {  infoReadOnly.value.first = \\'Arian\\'})

This won\\'t change the data and will warn you in the console as below:

\\\"Vue

Shallow Read Only

If we have ref and shallowRef, reactive and shallowReactive, why not have a shallowReadonly?

A shallowReadonly only makes the root level elements readonly whilst you can change the nested data:

const stateShallowReadonly = shallowReadonly({  name: \\'Adnan\\',  friends: [    { name: \\'Arian\\' },    { name: \\'Ata\\' },    { name: \\'Mahdi\\' }  ]})onMounted(() => {  stateShallowReadonly.name = \\'Brad\\'})

The code above will warn you and won\\'t change the value of name since it is a direct property.

But you can freely change anything inside friends since it is nested:

const stateShallowReadonly = shallowReadonly({  name: \\'Adnan\\',  friends: [    { name: \\'Arian\\' },    { name: \\'Ata\\' },    { name: \\'Mahdi\\' }  ]})onMounted(() => {  stateShallowReadonly.friends[0].name = \\'Brad\\'})

Computed

Man, I love computed in Vue! You can imagine it as a glass in which you can mix your potions and still have your potions standing there intact!

A computed is like a ref or reactive that can be accessed and watched but not changed. Then what\\'s the difference between a computed and a readonly you might ask. A computed can be a mixture of stuff. For example:

const state = ref({  first: \\'Adnan\\',  last: \\'Babakan\\'})const fullName = computed(() => state.value.first   \\' \\'   state.value.last)

Now you have a fullName which you may access its value inside a template with {{ fullName }} or inside your script using fullName.value.

The value of fullName will always depend on the state.value.first and state.value.last and will change if those guys change.

A computed receives a function that returns a value and can depend on multiple reactive data.

Writable Computed

Though a computed is mostly used to read a combination of data, the possibility to make a computed writable is also there.

Instead of passing a function to computed you may pass an object including two properties called get and set that both are functions.

For instance:

const state = ref({  first: \\'Adnan\\',  last: \\'Babakan\\'})const fullName = computed({  get: () => state.value.first   \\' \\'   state.value.last,  set: (value) => {    const [first, last] = value.split(\\' \\')    state.value.first = first    state.value.last = last  }})

Now if you try to write the value of fullName like below:

fullName.value = \\'Ata Parvin\\'

It will split your string into 2 parts using a space and assign the first part to state.value.first and the second to state.value.last.

This is not a good way to determine which part of a name is a first name and which is a last name, but for the sake of demonstration, this is the only thing that came to my mind. :D

Watching

Watching is something that you will probably need a lot. Watching is referred to the act in which you want to run something in case a reactive data changes. In different systems there are various naming for this act, sometimes they are called hooks as well. But in Vue, we will call them watches.

Watch

The first thing you will encounter. A watch function receives two arguments. The reactive data to watch and the function to be invoked when the data changes respectively.

Here is a simple watch:

const count = ref(0)const increase = () => {  count.value  }watch(count, () => {  console.log(\\'Count changed to \\'   count.value)})

Now whenever the value of count is changed, you will see the log Count changed to ((count)) in your console.

The callback function also receives two arguments which are passed to it when the watch is triggered. The first argument holds the new value and the second one holds the old value. Here is an example:

const count = ref(0)const increase = () => {  count.value  }watch(count, (newValue, oldValue) => {  console.log(\\'Counter changed from \\'   oldValue   \\' to \\'   newValue)})

Note: Be careful when using the newValue and oldValue with objects as objects are passed by reference.

To be more accurate, a watch function receives a third argument as well. This third argument is an object that holds some options which can change the behaviour of the watching action.

Immediate

An immediate watch function is triggered at the instance it\\'s created as well as when a change happens. You can think of it as the difference between a while loop and a do...while loop if you know what I mean. In other words, even if there is never a change, your callback function will run at least once:

watch(count, () => {  console.log(\\'Count changed to \\'   count.value)}, {  immediate: true,})

The value for immediate can be true or false. And the default value is false.

Once

If you want your watcher to run only once, you may define the once option and set its value to true. The default value is false.

watch(count, () => {  console.log(\\'Count changed to \\'   count.value)}, {  once: true,})

This will only trigger once when the value of count changes.

Advanced Watchers

Previously we\\'ve mentioned that watchers accept a reactive data as the first argument. While this is true, this is not the whole case.

A watch function can receive a getter function or an array of reactive objects and getter functions. This is used for when we need to watch for multiple data changes, and/or when we need to watch the result of two or more things when affecting each other. Let\\'s have some examples.

Watching Getter Function

Take the code below as an example:

It\\'s a simple code that makes 2 refs holding a number and increasing both of them 1 by 1 each second. Logically the difference of these two refs are always equal to zero unless one gets changes out of its turn. As both increase the difference stays 0 so the watched won\\'t get triggered as it only watches for the changes to the result of timerOne.value - timerTwo.value.

Yet there are two buttons that each adds 1 to timerOne and timerTwo respectively. When you click on any of those buttons the difference will be more or less than 0 thus the watch being triggered and logging the gap between these two timers.

Watching Multiple Values

Here is an example of an array of reactive data being passed to the first argument of the watch function:

No matter which ref changes, the watcher will be triggered.

Watch Effect

A watchEffect function acts almost exactly like a watch function with a main difference. In a watchEffect you don\\'t need to define what to watch and any reactive data that is used inside the callback you provide your watchEffect is watched automatically. For example:

const count = ref(0)watchEffect(() => {  console.log(count.value)})

In case our count is changed the watchEffect will trigger its callback function since count.value is used inside it. This is good if you have complex logic to watch for.


Hope this was useful and you\\'ve enjoyed it. In case you spot any mistakes or feel like there should be an improvement, kindly let me know.


BTW! Check out my free Node.js Essentials E-book here:

Feel free to contact me if you have any questions or suggestions.

","image":"http://www.luping.net/uploads/20241025/1729844286671b543e59632.gif","datePublished":"2024-11-07T22:30:03+08:00","dateModified":"2024-11-07T22:30:03+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > Vue 黑暗面備忘錄 |部分反應性

Vue 黑暗面備忘錄 |部分反應性

發佈於2024-11-07
瀏覽:833

Hi there DEV.to community!

This article will include multiple aspects of Vue 3 that are mostly used or are kind of on the dark side and not paid attention to as they are supposed to.

As I am going to describe Vue 3, I am going to use the composition API and not the old-school options API. The concept of both methods are same so you can adapt to composition API pretty quickly. I am in no place to dictate anything and every programmer is free to choose the way they want to write their program but as a personal opinion, I prefer composition API for its concise syntax and better code management. So if you are still afraid to change to composition API I suggest you give it a shot as it will be worth it.

Vue  Cheat Sheet of The Dark Side | Part  Reactivity

This is how you will feel after reading this article. I hope :).

Table of Content

  • Reactivity
    • Ref
    • Reactive
    • Shallow Ref
    • Shallow Reactive
    • Shallow Reactive
    • Readonly
    • Shallow Read Only
  • Computed
    • Writable Computed
  • Watching
    • Watch
      • Immediate
      • Once
      • Watching Getter Function
      • Watching Multiple Values
    • Watch Effect

Reactivity

React isn't the only one that should come to mind when we talk about reactivity. Reactivity refers to the ability of an entity (given a webpage) to react based on the data changes. You might know this concept as MVVM. MVVM is the abbreviated form of Model-View-View-Model. As the name suggests when data changes the view changes and vice-versa.

To utilize the reactivity power of Vue there are some options that we are going cover.

Ref

You can think of a ref as a special kind of variable that you can use inside your Vue application. This description is only true when you start working with Vue for the first time as it gets a bit more complex afterwards.

Defining a ref is as simple as this:

const message = ref("Hello World")

Use it inside your template using the interpolation syntax:

{{ message }}

If you are asking yourself why I called it a variable but declared message using a const keyword you have the every right to.

As you know you cannot change the value of constant, as it is the purpose of the const keyword. But there is subtle little thing you should know about. Although the keyword const doesn't let the data of a variable to be changed, it doesn't care about the nested data! This is the case with ref as well. To understand this situation better try the code below:

const o = {
  a: 1,
  b: 2,
  c: 3
}
console.log(o.a) // 1
o.a = 4
console.log(o.a) // 4

As you can see I can change the value of o.a since objects are only references and not a whole value by themselves. So when I change the value of a inside the object o the const limitation isn't applied. Of course, if you wanted to assign a value to o itself it will throw an error and won't let you do it. For example, the code below is wrong:

const o = {
  a: 1,
  b: 2,
  c: 3
}
o = "hello"

This is the same case when using ref (and other stuff you will see later here). When you invoke a ref function it turns everything it received into an object. This is called wrapping. Try this code:

const message = ref("Hello World")
console.log(message)

You should see something like the image below:

Vue  Cheat Sheet of The Dark Side | Part  Reactivity

As you can see when logging the message variable you are not receiving Hello World directly, instead it is inside an object and you can access your actual value using the value key of the aforementioned object. This lets Vue watch for changes and do the MVVM thing! :)

When you access your ref inside a Vue template there is no need to access it like message.value. Vue is smart enough to render the actual value inside the template instead of the object. But in case you want to access or modify the value of a ref inside your script you should do so using .value:

message.value = "Adnan!"
console.log(message.value) // Adnan!

Reactive

As you've seen when using a ref, Vue wraps your data inside an object and lets you access it via .value. This is usually the most used case. You can wrap almost everything using a ref and make it reactive.

In case you wonder how Vue watches for value changes and renders the view again and again, you should check out JavaScript Proxies.

If your value is an object itself, then you can use reactive instead of ref. The reactive function won't wrap your value and instead will make the object itself reactive and watchable.

const o = reactive({count: 0})

If you try to print out the o constant you will see that it is indeed your object without any major changes:

Vue  Cheat Sheet of The Dark Side | Part  Reactivity

Now you may manipulate the key count as you would normally do in JavaScript and Vue will render the changes as soon as possible.

Here is an example:

const increase = () => {
    o.count  
}

If you had ref instead of reactive it would have looked like this:

const o = ref({count: 0})

const increase = () => {
    o.value.count  
}

If you are still unsure which one to use, keep in mind that ref is a safe option to use.

Shallow Ref

Give that you have a ref like below:

const state = ref({
  names: {
    adnan: 'babakan',
    arian: 'amini',
    ata: 'parvin',
    mahdi: 'salehi'
  }
})

And printed my last name in your template as below:

{{ state.names.adnan }}

If you every changed my last name like this:

state.value.names.adnan = 'masruri'

Your template will be updated to show masruri instead of babakan. This is due to the fact that ref makes a deeply watched object and the changes to the view (template) are triggered for nested data as well.

There is an option to prevent such behaviour if that's what you want. To do so you may use shallowRef. A shallowRef acts exactly like ref does, with an exception of not watching for deep changes.

const state = shallowRef({
  names: {
    adnan: 'babakan',
    arian: 'amini',
    ata: 'parvin',
    mahdi: 'salehi'
  }
})

onMounted(() => {
  state.value.names.adnan = 'masruri'
})

The code above will result in your template showing babakan as it is not watched. But changing the .value entirely will trigger changes. So the code below will result in your template getting updated as well:

const state = shallowRef({
  names: {
    adnan: 'babakan',
    arian: 'amini',
    ata: 'parvin',
    mahdi: 'salehi'
  }
})

onMounted(() => {
  state.value = {
    names: {
      adnan: 'masruri',
      arian: 'amini',
      ata: 'parvin',
      mahdi: 'salehi'
    }
  }
})

This is a great option for performance-related concerns.

Shallow Reactive

So far we've known that ref wraps the data and watches it deeply and shallowRef wraps the data and watches it shallowly. Now tell me this, if reactive makes an object reactive, what does shallowReactive do?

const state = shallowReactive({
  names: {
    adnan: 'babakan',
    arian: 'amini',
    ata: 'parvin',
    mahdi: 'salehi',
  },
})

onMounted(() => {
  state.names.adnan = 'masruri'
})

As you might have guessed the template won't be updated.

Trigger Ref

Given that you are using a shallowRef and changed a value and now want your template to be updated according to the new data as well, you may use the triggerRef function:

const state = shallowRef({
  names: {
    adnan: 'babakan',
    arian: 'amini',
    ata: 'parvin',
    mahdi: 'salehi'
  }
})

onMounted(() => {
  state.value.names.adnan = 'masruri'
  triggerRef(state)
})

Now the template will also show masruri. This is more like changing from an automatic gear to a manual gear if you will.

This is usable for both shallowRef and shallowReactive.

Read Only

The readonly function receives a ref or a reactive as an argument and returns an exact copy that is only possible to be read from. This is used when you want to make sure your data is safe and is not possible to change when watching for it.

Example:

Though I've changed the value of info, since infoReadOnly is actually a live copy of info my changes are reflected in infoReadOnly as well. Yet you may not change the values using infoReadOnly directly:

const info = ref({
  first: 'Adnan',
  last: 'Babakan'
})

const infoReadOnly = readonly(info)

onMounted(() => {
  infoReadOnly.value.first = 'Arian'
})

This won't change the data and will warn you in the console as below:

Vue  Cheat Sheet of The Dark Side | Part  Reactivity

Shallow Read Only

If we have ref and shallowRef, reactive and shallowReactive, why not have a shallowReadonly?

A shallowReadonly only makes the root level elements readonly whilst you can change the nested data:

const stateShallowReadonly = shallowReadonly({
  name: 'Adnan',
  friends: [
    { name: 'Arian' },
    { name: 'Ata' },
    { name: 'Mahdi' }
  ]
})

onMounted(() => {
  stateShallowReadonly.name = 'Brad'
})

The code above will warn you and won't change the value of name since it is a direct property.

But you can freely change anything inside friends since it is nested:

const stateShallowReadonly = shallowReadonly({
  name: 'Adnan',
  friends: [
    { name: 'Arian' },
    { name: 'Ata' },
    { name: 'Mahdi' }
  ]
})

onMounted(() => {
  stateShallowReadonly.friends[0].name = 'Brad'
})

Computed

Man, I love computed in Vue! You can imagine it as a glass in which you can mix your potions and still have your potions standing there intact!

A computed is like a ref or reactive that can be accessed and watched but not changed. Then what's the difference between a computed and a readonly you might ask. A computed can be a mixture of stuff. For example:

const state = ref({
  first: 'Adnan',
  last: 'Babakan'
})

const fullName = computed(() => state.value.first   ' '   state.value.last)

Now you have a fullName which you may access its value inside a template with {{ fullName }} or inside your script using fullName.value.

The value of fullName will always depend on the state.value.first and state.value.last and will change if those guys change.

A computed receives a function that returns a value and can depend on multiple reactive data.

Writable Computed

Though a computed is mostly used to read a combination of data, the possibility to make a computed writable is also there.

Instead of passing a function to computed you may pass an object including two properties called get and set that both are functions.

For instance:

const state = ref({
  first: 'Adnan',
  last: 'Babakan'
})

const fullName = computed({
  get: () => state.value.first   ' '   state.value.last,
  set: (value) => {
    const [first, last] = value.split(' ')
    state.value.first = first
    state.value.last = last
  }
})

Now if you try to write the value of fullName like below:

fullName.value = 'Ata Parvin'

It will split your string into 2 parts using a space and assign the first part to state.value.first and the second to state.value.last.

This is not a good way to determine which part of a name is a first name and which is a last name, but for the sake of demonstration, this is the only thing that came to my mind. :D

Watching

Watching is something that you will probably need a lot. Watching is referred to the act in which you want to run something in case a reactive data changes. In different systems there are various naming for this act, sometimes they are called hooks as well. But in Vue, we will call them watches.

Watch

The first thing you will encounter. A watch function receives two arguments. The reactive data to watch and the function to be invoked when the data changes respectively.

Here is a simple watch:

const count = ref(0)

const increase = () => {
  count.value  
}

watch(count, () => {
  console.log('Count changed to '   count.value)
})

Now whenever the value of count is changed, you will see the log Count changed to ((count)) in your console.

The callback function also receives two arguments which are passed to it when the watch is triggered. The first argument holds the new value and the second one holds the old value. Here is an example:

const count = ref(0)

const increase = () => {
  count.value  
}

watch(count, (newValue, oldValue) => {
  console.log('Counter changed from '   oldValue   ' to '   newValue)
})

Note: Be careful when using the newValue and oldValue with objects as objects are passed by reference.

To be more accurate, a watch function receives a third argument as well. This third argument is an object that holds some options which can change the behaviour of the watching action.

Immediate

An immediate watch function is triggered at the instance it's created as well as when a change happens. You can think of it as the difference between a while loop and a do...while loop if you know what I mean. In other words, even if there is never a change, your callback function will run at least once:

watch(count, () => {
  console.log('Count changed to '   count.value)
}, {
  immediate: true,
})

The value for immediate can be true or false. And the default value is false.

Once

If you want your watcher to run only once, you may define the once option and set its value to true. The default value is false.

watch(count, () => {
  console.log('Count changed to '   count.value)
}, {
  once: true,
})

This will only trigger once when the value of count changes.

Advanced Watchers

Previously we've mentioned that watchers accept a reactive data as the first argument. While this is true, this is not the whole case.

A watch function can receive a getter function or an array of reactive objects and getter functions. This is used for when we need to watch for multiple data changes, and/or when we need to watch the result of two or more things when affecting each other. Let's have some examples.

Watching Getter Function

Take the code below as an example:

It's a simple code that makes 2 refs holding a number and increasing both of them 1 by 1 each second. Logically the difference of these two refs are always equal to zero unless one gets changes out of its turn. As both increase the difference stays 0 so the watched won't get triggered as it only watches for the changes to the result of timerOne.value - timerTwo.value.

Yet there are two buttons that each adds 1 to timerOne and timerTwo respectively. When you click on any of those buttons the difference will be more or less than 0 thus the watch being triggered and logging the gap between these two timers.

Watching Multiple Values

Here is an example of an array of reactive data being passed to the first argument of the watch function:

No matter which ref changes, the watcher will be triggered.

Watch Effect

A watchEffect function acts almost exactly like a watch function with a main difference. In a watchEffect you don't need to define what to watch and any reactive data that is used inside the callback you provide your watchEffect is watched automatically. For example:

const count = ref(0)

watchEffect(() => {
  console.log(count.value)
})

In case our count is changed the watchEffect will trigger its callback function since count.value is used inside it. This is good if you have complex logic to watch for.


Hope this was useful and you've enjoyed it. In case you spot any mistakes or feel like there should be an improvement, kindly let me know.


BTW! Check out my free Node.js Essentials E-book here:

Feel free to contact me if you have any questions or suggestions.

版本聲明 本文轉載於:https://dev.to/adnanbabakan/vue-3-cheat-sheet-of-the-dark-side-part-1-reactivity-5a89?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>
  • 最佳化效能:為資料透視表選擇最佳資料來源
    最佳化效能:為資料透視表選擇最佳資料來源
    TL;DR: Syncfusion Pivot Table connects to multiple data sources, making it a versatile tool for data analysis. Selecting the right data source is cruc...
    程式設計 發佈於2024-11-08
  • 使用 Secrets Loader 輕鬆管理 Laravel 和 JS 項目
    使用 Secrets Loader 輕鬆管理 Laravel 和 JS 項目
    跨各种环境管理 API 密钥、令牌和凭证等敏感数据可能非常棘手,尤其是在开发和部署应用程序时。确保秘密在需要时安全地存储和获取,而不是将它们硬编码到版本控制中,对于维护安全性至关重要。 这就是为什么我创建了 Secrets Loader,这是一个 Bash 脚本,可以动态地将 AWS SSM 和 C...
    程式設計 發佈於2024-11-08
  • 如何在 Android 中正確實作 CheckBox 的偵聽器?
    如何在 Android 中正確實作 CheckBox 的偵聽器?
    Android 中的CheckBox 偵聽器Android 中的CheckBox 偵聽器在Android 中實作CheckBox 偵聽器時,必須解決使用標準時面臨的常見問題OnCheckedChangeListener類。 satView.setOnCheckedChangeListener(new...
    程式設計 發佈於2024-11-08
  • Firestore 如何優化社群網路時間軸以實現可擴充性?
    Firestore 如何優化社群網路時間軸以實現可擴充性?
    使用Firestore 優化社交網路時間軸在設計具有提要和關注功能的社交網路時,資料庫可擴展性對於處理潛在問題至關重要大型數據集。 Firebase 的即時資料庫帶來了可擴展性挑戰,特別是在儲存使用者時間軸的方法方面。要解決這些問題,請考慮過渡到 Firestore。 優化的資料庫結構Firesto...
    程式設計 發佈於2024-11-08
  • 如何解決將物件數組作為函數參數傳遞時的錯誤?
    如何解決將物件數組作為函數參數傳遞時的錯誤?
    類型提示:物件陣列將物件陣列作為參數傳遞給函數時,如果未指定參數類型。例如,考慮以下程式碼:class Foo {} function getFoo(Foo $f) {}嘗試將 Foo 物件陣列傳遞給 getFoo 將導致致命錯誤:Argument 1 passed to getFoo() must...
    程式設計 發佈於2024-11-08
  • 為什麼 iOS 裝置上缺少 CSS 捲軸?
    為什麼 iOS 裝置上缺少 CSS 捲軸?
    iOS上無法顯示有CSS Overflow的捲軸為iPad開發網站時,使用CSS屬性overflow: auto來啟用div內的捲軸可能無效。儘管兩指滾動手勢功能正常,但捲軸仍然隱藏。嘗試同時使用溢出:自動和溢出:滾動不會產生任何結果。 iOS行為不幸的是,溢位:自動和捲動都不會在iOS裝置上產生捲...
    程式設計 發佈於2024-11-08
  • Java中如何從執行緒操作傳回值?
    Java中如何從執行緒操作傳回值?
    執行緒操作回傳值在多執行緒程式設計中,執行緒之間的互動往往需要交換資料。常見的情況是嘗試檢索在單獨執行緒中執行的操作的結果。 請考慮下面的範例程式碼:public void test() { Thread uiThread = new HandlerThread("UIHandle...
    程式設計 發佈於2024-11-08
  • Python 簡介:)
    Python 簡介:)
    歷史 Python 由 Guido van Rossum 創建,首次發佈於 1991 年。它旨在優先考慮程式碼的可讀性和簡單性,從而提高開發人員的工作效率。 「Python」 的靈感來自 BBC 電視節目 「Monty Python's Flying Circus」,van...
    程式設計 發佈於2024-11-08
  • 學習 Go 結構最終如何讓我愛上編碼
    學習 Go 結構最終如何讓我愛上編碼
    「我仍然記得早期與代碼搏鬥的日子。 基本的東西?我正要到那裡。但後來出現了結構體,一切都變得模糊起來。我不斷地破壞東西,我的程式碼一團糟。我做錯了什麼? 直到我坐下來,學習了 Go 結構體的基礎知識,並開始有效地使用它們,事情才終於有了進展。那是轉捩點。突然間,程式碼變得更有組織、更有效率、更乾淨...
    程式設計 發佈於2024-11-08
  • MERN 堆疊仍然有效嗎?
    MERN 堆疊仍然有效嗎?
    Remember when the MERN stack was the Beyoncé of web development stacks? It was everywhere, and if you weren’t using it, you were probably missing out ...
    程式設計 發佈於2024-11-08
  • 什麼時候需要在 Tkinter 中呼叫 `mainloop()`?
    什麼時候需要在 Tkinter 中呼叫 `mainloop()`?
    在 Tkinter 應用程式中呼叫 mainloop在 Tkinter 中,mainloop 是實現視窗渲染和事件處理的基本功能。與流行的看法相反,並不總是需要在互動式 shell 環境中明確呼叫 mainloop。然而,這種便利性在 shell 之外並不適用。 mainloop 的角色mainlo...
    程式設計 發佈於2024-11-08
  • 如何解決將靜態 C 庫與 C++ 程式碼連結時出現「未定義的引用」錯誤?
    如何解決將靜態 C 庫與 C++ 程式碼連結時出現「未定義的引用」錯誤?
    對用C 程式碼連結靜態C 函式庫時的錯誤的未定義引用當嘗試用C 程式碼連結靜態C 函式庫時,您可以儘管修改了連結順序,但仍遇到「未定義的引用」錯誤。此問題是由 C 和 C 編譯創建的不同符號名稱(稱為“名稱修飾”)引起的。 在 C 中,連結器在錯誤訊息中顯示分解的符號名稱,這可能會造成混淆。使用“n...
    程式設計 發佈於2024-11-08
  • 書籍:學習 JavaScript 設計模式
    書籍:學習 JavaScript 設計模式
    本書探討了 JavaScript 中常見軟體設計模式的實作和使用。雖然根據最新的最佳實踐,一些示例可能稍微過時,但它們對於維護遺留系統的人來說仍然很有價值。 對於初學者: 它是對軟體設計模式的出色介紹。然而,對於那些程式設計經驗有限的人來說,這些模式解決的問題可能不太熟悉。 ...
    程式設計 發佈於2024-11-08
  • 了解命令式程式設計和聲明式程式設計之間的區別
    了解命令式程式設計和聲明式程式設計之間的區別
    當我剛開始學習React時,我的老師說:「JavaScript是命令式編程,而React是聲明式編程。」然而,一開始這對我來說不太有意義。因此,我決定將其分解以更好地理解其中的差異。 將命令式和聲明式程式設計與披薩進行比較? 為了更容易理解,讓我們來比較一下這兩種烹飪方法。 ...
    程式設計 發佈於2024-11-08
  • 如何在JavaScript中進行穩定排序以保持元素順序的一致性?
    如何在JavaScript中進行穩定排序以保持元素順序的一致性?
    JavaScript 中的穩定排序演算法對資料進行排序時,保留相等元素的原始順序對於穩定的排序演算法至關重要。在這種情況下,我們的目標是按給定順序對具有特定鍵的物件陣列進行排序,同時保持元素順序一致性。 穩定排序技術有趣的是,甚至非穩定排序函數可以實現穩定排序。透過在排序前捕捉每個元素的初始位置,我...
    程式設計 發佈於2024-11-08

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3