- Building Web and Mobile ArcGIS Server Applications with JavaScript(Second Edition)
- Eric Pimpler Mark Lewin
- 450字
- 2021-07-02 15:48:54
Variable data types
JavaScript supports various types of data that can be assigned to your variables. Unlike other strongly typed languages such as .NET or C++, JavaScript is a loosely typed language. What this means is that you don't have to specify the type of data that will occupy your variable. The JavaScript interpreter does this for you on the fly. You can assign strings of text, numbers, Boolean true/false values, arrays, or objects to your variables.
Numbers and strings are pretty straight forward for the most part. Strings are simply text enclosed by either a single or a double quote. For instance:
var baseMapLayer = "Terrain";
var operationalLayer = 'Parcels';
Numbers are not enclosed inside quote marks and can be integers or floating point numbers:
var currentMonth = 12;
var layered = 3;
var speed = 34.35;
One thing we would point out to new programmers is that numeric values can be assigned to string variables through the use of single or double quotes that enclose the value. This can be confusing at times for some new programmers. For instance, a value of 3.14 without single or double-quotes is a numeric data type while a value of "3.14" with single or double quotes is assigned a string data type.
Other data types include Booleans that are simply true or false values, and arrays that are a collection of data values. An array basically serves as a container for multiple values. For instance, you could store a list of geographic data layer names within an array and access each element in the array individually as required.
Arrays allow you to store multiple values in a single variable. For example, you might want to store the names of all the layers you want to add to a map. Rather than creating individual variables for each layer you could use an array to store all of them in a single variable. You can then reference individual values from the array using an index number, or by looping through them with a for loop. The following code example shows how to create an array in JavaScript and reference its individual members via their index values:
var myLayers=new Array(); myLayers[0]="Parcels"; myLayers[1]="Streets"; myLayers[2]="Streams";
You could also simplify the creation of this array variable as seen in the following code example where the array has been created as a comma-separated list enclosed in brackets:
var myLayers = ["Parcels", "Streets", "Streams"];
Bear in mind that if you access array elements via their index, the index numbering is zero-based. This means that the first item in the array occupies position 0 and each successive item in the array is incremented by one:
var layerName = myLayers[0]; //returns Parcels
- 黑客攻防從入門到精通(實戰秘笈版)
- 新編Visual Basic程序設計上機實驗教程
- 軟件架構設計:大型網站技術架構與業務架構融合之道
- Windows系統管理與服務配置
- Learning Bayesian Models with R
- 數據庫系統原理及MySQL應用教程
- 你必須知道的204個Visual C++開發問題
- 劍指MySQL:架構、調優與運維
- 深入理解Elasticsearch(原書第3版)
- Swift Playgrounds少兒趣編程
- Python極簡講義:一本書入門數據分析與機器學習
- Natural Language Processing with Java and LingPipe Cookbook
- LabVIEW虛擬儀器入門與測控應用100例
- HTML5開發精要與實例詳解
- Mudbox 2013 Cookbook