


An in-depth analysis of data sharing and data transfer in JavaScript_javascript skills
データ共有とデータ転送は相互に補完的なものです。この問題について一緒に議論しましょう。まず最初に言っておきたいのは、共有と受け渡しの両方に範囲があるということです。スコープとは、同じスコープ内のデータを共有できる範囲のことであり、このスコープを超える場合は、クロススコープを使用する必要があります。
範囲
1.ui スコープ
各 ui ファイルには、デフォルトで対応する ui.js があります。これらは閉じたスコープとして機能します。 ui.js では、ui オブジェクトは ui ファイル内のコンポーネントの ID に基づいて取得されます。異なる ui ファイルが同じ ID を持つコンポーネントを定義できます。 ui.js で定義された変数には、この js でのみアクセスできます。
2.ページスコープ
openPage が呼び出されるたびに、新しいページが開かれ、この新しいページは古いページで覆われ、closePage が閉じた後、覆われた古いページが表示されます。メインの UI ファイルに加えて、各ページには他の多くの UI ファイルを含めることもでき、これらの UI ファイルは同じページ スコープ内にあります。
ページを閉じると、ページ内に構築されているすべてのオブジェクトが解放されます。
3.アプリのスコープ
これは最大のスコープであり、アプリが終了しない限り、このスコープは常に有効です。
app.js はどのページにも属していないため、アプリ スコープに属します。
つまり、app スコープには複数のページ スコープが含まれ、ページ スコープには複数の ui スコープが含まれます。
メモリ共有
ファイルやデータベースと比較して、メモリ操作ははるかに高速であり、比較的少量のデータの操作に適しています。欠点は、アプリを閉じた後に解放されることです。 deviceone は次の方法でメモリを共有します。
1. do_Global のメモリ操作 (アプリスコープ)
これはアプリ範囲のデータ共有です。このメモリ部分は実際にはキーと値のペアであり、1 つのキーが 1 つの値に対応するため、キーを再割り当てすると、以前の値が上書きされることに注意してください。使用方法はとても簡単です。次の例を参照してください。読み取りと書き込みは別のページにあります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
2. Javascript グローバル変数 (ページスコープ)
JavaScript の特性を使用してグローバル変数を定義する 通常、同じページ上の異なる UI ファイルでデータを共有するためにグローバル変数を定義できます。次の例を参照してください。読み取りと書き込みは異なる ui ファイル内で行われますが、同じページ スコープ内にあります。使い方も非常に簡単で、次の 2 つの方法があります:
非常に便利ですが、共同開発や複雑なプロジェクトの場合、バグを見つけてデバッグするのが困難になるため、使用することはお勧めできません。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
3. Javascript 変数 (UI スコープ)
これについては多くの説明は必要ありません。これは通常の js 変数定義であり、現在の ui.js スコープでのみ有効です。
1 |
|
4. sqlite のメモリモード
SQLite は通常はファイル モードですが、SQLite をメモリ内で直接使用できるため、SQL ステートメントを使用すると操作がより柔軟になります。
メモリ モードは 1 つだけあり、名前は:memory: に固定されます。
sqliteデータベースの紹介で後ほど詳しく紹介します。
ファイル共有
これは理解するのが簡単です。ファイル共有はアプリスコープであり、アプリの再起動後にアクセスできます。 do_Storage コンポーネントを使用すると、アプリ内の任意の場所にコンテンツをファイルに書き込み、別の場所にあるファイルからコンテンツを読み取ることができます。次の例を参照してください。読み取りと書き込みは別のページにあります。ここで注意すべき点は、ファイルの読み取りと書き込みは通常は非同期であるということです。
を読み取る前に、その内容が書き込まれていることを確認する必要があります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
do_SQLite コンポーネントがデータベース データにアクセスします
このコンポーネントは MM コンポーネントです。つまり、複数のインスタンスを作成できます。すべての MM コンポーネントはデフォルトでページ スコープですが、アプリ スコープにすることもできます。 MM コンポーネント作成の 3 番目のパラメーターはスコープを示します。
ここで、SQLite の読み取りと書き込みは通常は非同期であることに注意してください。読み取る前に、コンテンツが書き込まれていることを確認する必要があります。
1. アプリのスコープ:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
2. ページ範囲:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
数据传递
数据传递涉及到跨作用域,比如不同的ui文件传递数据,不同的page传递数据。
其中最重要也是最常用的方式就是消息机制
1.消息机制
这个环节我们在文档再里详细介绍。
总之,消息机制可以在跨ui作用域传递数据,也可以跨page作用域传递数据。
2.openPage和closePage传递数据。
这个数据传递是跨page作用域,但是只限于相隔二层page之间。比如在page1的基础上打开page2,page1把一些数据传递给page2;page2关闭自身,露出page1,又可以把数据传递回page1. 数据传递可以是任何json对象。
这是一个常规而且非常好的方式,建议都这么使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
关于本文给大家介绍的js数据共享和数据传递的相关知识就给大家介绍这么多,希望对大家有所帮助!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

This article explores effective use of Java's Collections Framework. It emphasizes choosing appropriate collections (List, Set, Map, Queue) based on data structure, performance needs, and thread safety. Optimizing collection usage through efficient

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript
