jsduck 3.0.pre → 3.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +21 -229
- data/Rakefile +31 -17
- data/bin/jsduck +1 -0
- data/js-classes/Array.js +561 -0
- data/js-classes/Boolean.js +110 -0
- data/js-classes/Date.js +999 -0
- data/js-classes/Function.js +256 -0
- data/js-classes/Number.js +308 -0
- data/js-classes/Object.js +404 -0
- data/js-classes/RegExp.js +415 -0
- data/js-classes/String.js +1034 -0
- data/jsduck.gemspec +2 -2
- data/lib/jsduck/accessors.rb +71 -0
- data/lib/jsduck/aggregator.rb +14 -2
- data/lib/jsduck/app.rb +6 -5
- data/lib/jsduck/class_formatter.rb +12 -8
- data/lib/jsduck/css_parser.rb +2 -2
- data/lib/jsduck/doc_formatter.rb +2 -2
- data/lib/jsduck/doc_parser.rb +32 -25
- data/lib/jsduck/exporter.rb +1 -1
- data/lib/jsduck/guides.rb +11 -2
- data/lib/jsduck/js_parser.rb +30 -8
- data/lib/jsduck/merger.rb +31 -16
- data/lib/jsduck/options.rb +93 -15
- data/lib/jsduck/renderer.rb +40 -3
- data/lib/jsduck/search_data.rb +8 -5
- data/lib/jsduck/source_file.rb +5 -4
- data/lib/jsduck/type_parser.rb +7 -6
- metadata +17 -5
- data/example.js +0 -144
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
JsDuck
|
2
2
|
======
|
3
3
|
|
4
|
-
API documentation generator for
|
4
|
+
API documentation generator for Ext JS 4.
|
5
5
|
|
6
6
|
,~~.
|
7
7
|
( 6 )-_,
|
@@ -10,53 +10,19 @@ API documentation generator for ExtJS 4.
|
|
10
10
|
\ `-' / hjw
|
11
11
|
~'`~'`~'`~'`~
|
12
12
|
|
13
|
-
JsDuck aims to be a better documentation generator for [
|
14
|
-
the old [ext-doc][] was.
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
HTML, it prefers a friendlier [Markdown][] syntax:
|
23
|
-
|
24
|
-
/**
|
25
|
-
* Basic text field. Can be used as a direct replacement for traditional
|
26
|
-
* text inputs, or as the base class for more sophisticated input controls
|
27
|
-
* (like Ext.form.TextArea and Ext.form.ComboBox).
|
28
|
-
*
|
29
|
-
* Validation
|
30
|
-
* ----------
|
31
|
-
*
|
32
|
-
* The validation procedure is described in the documentation for
|
33
|
-
* {@link #validateValue}.
|
34
|
-
*
|
35
|
-
* Alter Validation Behavior
|
36
|
-
* -------------------------
|
37
|
-
*
|
38
|
-
* Validation behavior for each field can be configured:
|
39
|
-
*
|
40
|
-
* - `{@link Ext.form.TextField#invalidText invalidText}` :
|
41
|
-
* the default validation message to show if any validation step above
|
42
|
-
* does not provide a message when invalid
|
43
|
-
* - `{@link Ext.form.TextField#maskRe maskRe}` :
|
44
|
-
* filter out keystrokes before any validation occurs
|
45
|
-
* - `{@link Ext.form.TextField#stripCharsRe stripCharsRe}` :
|
46
|
-
* filter characters after being typed in, but before being validated
|
47
|
-
*
|
48
|
-
* @xtype textfield
|
49
|
-
*/
|
50
|
-
Ext.define('Ext.form.field.Text', {
|
51
|
-
extend: 'Ext.form.field.Base',
|
52
|
-
|
53
|
-
As you can see, JsDuck can infer several things from the code (like
|
54
|
-
`@class` and `@extends` in this case), so you don't have to repeat
|
55
|
-
yourself.
|
56
|
-
|
57
|
-
[ExtJS]: http://www.sencha.com/products/js/
|
13
|
+
JsDuck aims to be a better documentation generator for [Ext JS][] than
|
14
|
+
the old [ext-doc][] was. It is used by Sencha to generate the official
|
15
|
+
[Ext JS 4 documentation][ext4-docs].
|
16
|
+
|
17
|
+
The highlights of JSDuck are [Markdown][] support and keeping you DRY
|
18
|
+
by inferring a lot of information from code. Read the [Guide][] for
|
19
|
+
full overview.
|
20
|
+
|
21
|
+
[Ext JS]: http://www.sencha.com/products/js/
|
58
22
|
[ext-doc]: http://ext-doc.org/
|
59
23
|
[Markdown]: http://daringfireball.net/projects/markdown/
|
24
|
+
[ext4-docs]: http://docs.sencha.com/ext-js/4-0/
|
25
|
+
[Guide]: https://github.com/senchalabs/jsduck/wiki/Guide
|
60
26
|
|
61
27
|
|
62
28
|
Getting it
|
@@ -120,100 +86,23 @@ documentation, but if your project is built on top of it, it makes
|
|
120
86
|
sense to do so - otherwise you won't be able to see which methods your
|
121
87
|
classes inherit from Ext JS classes.
|
122
88
|
|
89
|
+
To create guides, videos and other sections, read about the
|
90
|
+
[Advanced Usage][adv] in wiki.
|
91
|
+
|
123
92
|
[official]: http://docs.sencha.com/ext-js/4-0/
|
93
|
+
[adv]: https://github.com/senchalabs/jsduck/wiki/Advanced-Usage
|
124
94
|
|
125
95
|
|
126
96
|
Hacking it
|
127
97
|
----------
|
128
98
|
|
129
|
-
|
130
|
-
|
131
|
-
$ git clone git://github.com/senchalabs/jsduck.git
|
132
|
-
|
133
|
-
JsDuck depends on [json][], [RDiscount][], and [parallel][]; plus
|
134
|
-
[RSpec][] for tests, [compass][] for compiling the stylesheets, and
|
135
|
-
[Sencha SDK tools][sdk-tools] for combining JavaScript files. Make
|
136
|
-
sure you have these installed:
|
137
|
-
|
138
|
-
$ gem list
|
139
|
-
|
140
|
-
First smoke test. Run the testsuite:
|
141
|
-
|
142
|
-
$ cd jsduck
|
143
|
-
$ rake
|
144
|
-
|
145
|
-
If everything is green continue with some configuration. Copy an
|
146
|
-
ExtJS 4 download inside template/extjs directory (you might just
|
147
|
-
create a symlink if you wish so):
|
148
|
-
|
149
|
-
$ cp -r path/to/ext-4.0.2a template/extjs
|
150
|
-
|
151
|
-
Now create a file `sdk-vars.rb` inside the jsduck directory. Use the
|
152
|
-
following template:
|
153
|
-
|
154
|
-
# path to Ext JS 4 build
|
155
|
-
EXT_DIR='/path/to/ext-4.0.2a'
|
156
|
-
# where to output the docs
|
157
|
-
OUT_DIR='/path/to/ouput/dir'
|
158
|
-
# path to SDK (for developers at Sencha)
|
159
|
-
SDK_DIR='/path/to/SDK'
|
160
|
-
|
161
|
-
These config options define the input and output directories for the
|
162
|
-
ext4 rake task. Leave out the SDK_DIR option if you are not a Sencha
|
163
|
-
developer. Now run the task:
|
164
|
-
|
165
|
-
$ rake ext4
|
166
|
-
|
167
|
-
This will generate a development/debug version of the docs app into
|
168
|
-
the output directory you specified in `sdk-vars.rb` file. Open up the
|
169
|
-
directory in your web browser and see if everything looks fine. (This
|
170
|
-
task only creates the class documentation, don't worry about guides,
|
171
|
-
examples, etc being missing, and the index page of api docs blank).
|
172
|
-
|
173
|
-
This rake task is most suitable when developing the JavaScript side of
|
174
|
-
the app, as it only symlinks the files in template/ directory,
|
175
|
-
allowing you to modify the files there and see the results by just
|
176
|
-
refreshing the browser window (and not having to run the rake task
|
177
|
-
again and again).
|
178
|
-
|
179
|
-
To generate a stand-alone version of documentation (which you could
|
180
|
-
upload to some server), add export parameter to the rake task:
|
181
|
-
|
182
|
-
$ rake ext4[export]
|
183
|
-
|
184
|
-
This will combine and compress the javascript files using Sencha SDK
|
185
|
-
Tools and does not create symlinks to the development directory.
|
186
|
-
|
187
|
-
For developing the Ruby side, try to please the unit tests.
|
188
|
-
|
189
|
-
If you are a Sencha developer, use the `sdk` and `touch` tasks instead
|
190
|
-
to generate either extjs or touch docs from the corresponding branches
|
191
|
-
in SDK repository.
|
192
|
-
|
193
|
-
Happy hacking.
|
194
|
-
|
195
|
-
[json]: http://flori.github.com/json/
|
196
|
-
[RDiscount]: https://github.com/rtomayko/rdiscount
|
197
|
-
[parallel]: https://github.com/grosser/parallel
|
198
|
-
[RSpec]: http://rspec.info/
|
199
|
-
[compass]: http://compass-style.org/
|
200
|
-
[sdk-tools]: http://www.sencha.com/products/sdk-tools/
|
201
|
-
|
202
|
-
|
203
|
-
Documenting your code with JSDuck
|
204
|
-
---------------------------------
|
99
|
+
See [Hacking guide](https://github.com/senchalabs/jsduck/wiki/Hacking) in wiki.
|
205
100
|
|
206
|
-
Here's an overview of [all the available @tags][tags], and how to use
|
207
|
-
them:
|
208
101
|
|
209
|
-
|
210
|
-
|
211
|
-
* [Config options](https://github.com/senchalabs/jsduck/wiki/Cfg)
|
212
|
-
* [Properties](https://github.com/senchalabs/jsduck/wiki/Property)
|
213
|
-
* [Methods](https://github.com/senchalabs/jsduck/wiki/Method)
|
214
|
-
* [Events](https://github.com/senchalabs/jsduck/wiki/Event)
|
102
|
+
Documenting your code
|
103
|
+
---------------------
|
215
104
|
|
216
|
-
[
|
105
|
+
All the supported syntax is described in the [Guide][].
|
217
106
|
|
218
107
|
|
219
108
|
Copying
|
@@ -236,101 +125,4 @@ reported bugs, submitted patches, and provided a lot of useful input.
|
|
236
125
|
Changelog
|
237
126
|
---------
|
238
127
|
|
239
|
-
|
240
|
-
* Tabs are back
|
241
|
-
* Separate front page
|
242
|
-
* Guides in separate section
|
243
|
-
* New Videos section
|
244
|
-
* New Examples section
|
245
|
-
* Inline examples
|
246
|
-
* Syntax for documenting object properties.
|
247
|
-
* Syntax for documenting default values.
|
248
|
-
* New Syntax for optional parameters.
|
249
|
-
* Syntax for required configs.
|
250
|
-
* Print view and support for Google AJAX crawling.
|
251
|
-
* No more --show-private-classes option, private classes now always shown.
|
252
|
-
* Lots and lots of other fixes...
|
253
|
-
|
254
|
-
* 2.0.pre4 - Fixes for the previous pre-release.
|
255
|
-
* Paging of search results.
|
256
|
-
* Support opening classes in tree in new tabs in IE and Firefox.
|
257
|
-
* Include upgraded version of prettifier, fixing formatting in IE.
|
258
|
-
* New --head-html and --body-html command line options.
|
259
|
-
* New --local-storage-db command line option.
|
260
|
-
* Avoid creating case-insensitively same source file names,
|
261
|
-
preventing duplicate file conflicts in Windows.
|
262
|
-
* Include missing ExtJS image files.
|
263
|
-
* Don't exclude static members from singleton classes, simply print
|
264
|
-
warning about using @static in singleton class.
|
265
|
-
|
266
|
-
* 2.0.pre3 - Bogus release, skipped.
|
267
|
-
|
268
|
-
* 2.0.pre2 - Fixes for the previous pre-release.
|
269
|
-
* New --stdout command line option.
|
270
|
-
* Fix opening links in new tabs.
|
271
|
-
* Few other small bugfixes and enhancements.
|
272
|
-
|
273
|
-
* 2.0.pre - Completely overhauled Ext4-themed version.
|
274
|
-
* A lot of changes since 0.6 releases.
|
275
|
-
|
276
|
-
* 0.6.1 - Bug fixes.
|
277
|
-
* Fix scrolling to class members in Chrome 12.
|
278
|
-
* Make JSDuck work with Ruby 1.8.6.
|
279
|
-
* Upgrade the embedded ExtJS to 3.4.0.
|
280
|
-
|
281
|
-
* 0.6 - JsDuck is now used for creating the official ExtJS4 documentation.
|
282
|
-
* Automatic linking of class names found in comments. Instead of writing
|
283
|
-
`{@link Ext.Panel}` one can simply write `Ext.Panel` and link will be
|
284
|
-
automatically created.
|
285
|
-
* In generated docs, method return types and parameter types are also
|
286
|
-
automatically linked to classes if such class is included to docs.
|
287
|
-
* Support for `{@img}` tag for including images to documentation.
|
288
|
-
The markup created by `{@link}` and `{@img}` tags can now be customized using
|
289
|
-
the --img and --link command line options to supply HTML templates.
|
290
|
-
* Links to source code are no more simply links to line numbers.
|
291
|
-
Instead the source code files will contain ID-s like `MyClass-cfg-style`.
|
292
|
-
* New tags: `@docauthor`, `@alternateClassName`, `@mixins`.
|
293
|
-
The latter two Ext4 class properties are both detected from code and
|
294
|
-
can also be defined (or overriden) in doc-comments.
|
295
|
-
* Global methods are now placed to separate "global" class.
|
296
|
-
Creation of this can be turned off using `--ignore-global`.
|
297
|
-
* Much improved search feature.
|
298
|
-
Search results are now ordered so that best matches are at the top.
|
299
|
-
No more is there a select-box to match at beginning/middle/end -
|
300
|
-
we automatically search first by exact match, then beginning and
|
301
|
-
finally by middle. Additionally the search no more lists a lot of
|
302
|
-
duplicates - only the class that defines a method is listed, ignoring
|
303
|
-
all the classes that inherit it.
|
304
|
-
* Support for doc-comments in [SASS](http://sass-lang.com/) .scss files:
|
305
|
-
For now, it's possible to document SASS variables and mixins.
|
306
|
-
* Several bug fixes.
|
307
|
-
|
308
|
-
* 0.5 - Search and export
|
309
|
-
* Search from the actually generated docs (not through sencha.com)
|
310
|
-
* JSON export with --json switch.
|
311
|
-
* Listing of mixed into classes.
|
312
|
-
* Option to control or disable parallel processing.
|
313
|
-
* Accepting directories as input (those are scanned for .js files)
|
314
|
-
* Many bug fixes.
|
315
|
-
|
316
|
-
* 0.4 - Ext4 support
|
317
|
-
* Support for Ext.define() syntax from ExtJS 4.
|
318
|
-
* Showing @xtype and @author information on generated pages.
|
319
|
-
* Showing filename and line number in warnings.
|
320
|
-
* Fix for event showing the same doc as method with same name.
|
321
|
-
|
322
|
-
* 0.3 - Performance improvements
|
323
|
-
* Significant peed improvements - most importantly utilizing
|
324
|
-
multiple CPU-s (if available) to speed things up. On my 4-core
|
325
|
-
box JsDuck is now even faster than ext-doc.
|
326
|
-
* Printing of performance info in verbose mode
|
327
|
-
* Support for comma-first coding style
|
328
|
-
* Few other fixes to JavaScript parsing
|
329
|
-
|
330
|
-
* 0.2 - most features of ext-doc supported.
|
331
|
-
* Links from documentation to source code
|
332
|
-
* Syntax highlighting of code examples
|
333
|
-
* Tree of parent classes
|
334
|
-
* List of subclasses
|
335
|
-
|
336
|
-
* 0.1 - initial version.
|
128
|
+
See [Changelog](https://github.com/senchalabs/jsduck/wiki/Changelog) page in wiki.
|
data/Rakefile
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'rake'
|
3
|
+
require 'json'
|
3
4
|
|
4
5
|
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
5
6
|
|
@@ -92,8 +93,6 @@ def compress
|
|
92
93
|
# Now do everything that follows in template-min/ dir
|
93
94
|
dir = "template-min"
|
94
95
|
|
95
|
-
# Create JSB3 file for Docs app
|
96
|
-
system("sencha", "create", "jsb", "-a", "#{dir}/build-js.html", "-p", "#{dir}/app.jsb3")
|
97
96
|
# Concatenate files listed in JSB3 file
|
98
97
|
system("sencha", "build", "-p", "#{dir}/app.jsb3", "-d", dir)
|
99
98
|
# Remove intermediate build files
|
@@ -161,12 +160,13 @@ class JsDuckRunner
|
|
161
160
|
"--head-html", head_html,
|
162
161
|
"--footer", "Ext JS 4.0.6 Docs - Generated with <a href='https://github.com/senchalabs/jsduck'>JSDuck</a> rev #{revision}",
|
163
162
|
"--welcome", "template/welcome.html",
|
164
|
-
"--guides", "#{@sdk_dir}/
|
165
|
-
"--videos", "#{@sdk_dir}/
|
163
|
+
"--guides", "#{@sdk_dir}/extjs/docs/guides.json",
|
164
|
+
"--videos", "#{@sdk_dir}/extjs/docs/videos.json",
|
166
165
|
"--examples", "#{@sdk_dir}/extjs/examples/examples.json",
|
167
|
-
"--inline-examples", "#{@sdk_dir}/extjs/
|
168
|
-
"--categories", "#{@sdk_dir}/extjs/
|
166
|
+
"--inline-examples", "#{@sdk_dir}/extjs/docs/resources",
|
167
|
+
"--categories", "#{@sdk_dir}/extjs/docs/categories.json",
|
169
168
|
"--output", "#{@out_dir}",
|
169
|
+
"--builtin-classes",
|
170
170
|
"#{@sdk_dir}/extjs/src",
|
171
171
|
"#{@sdk_dir}/platform/src",
|
172
172
|
"#{@sdk_dir}/platform/core/src",
|
@@ -197,16 +197,24 @@ class JsDuckRunner
|
|
197
197
|
"--categories", "#{@sdk_dir}/touch/doc-resources/categories.json",
|
198
198
|
"--videos", "#{@sdk_dir}/touch/doc-resources/videos.json",
|
199
199
|
"--output", "#{@out_dir}",
|
200
|
-
"
|
201
|
-
"#{@sdk_dir}/touch/src/data",
|
202
|
-
"#{@sdk_dir}/touch/src/gestures",
|
203
|
-
"#{@sdk_dir}/touch/src/layout",
|
204
|
-
"#{@sdk_dir}/touch/src/plugins",
|
205
|
-
"#{@sdk_dir}/touch/src/util",
|
206
|
-
"#{@sdk_dir}/touch/src/widgets",
|
207
|
-
"#{@sdk_dir}/touch/src/platform/src",
|
200
|
+
"--external=google.maps.Map,google.maps.LatLng",
|
208
201
|
"#{@sdk_dir}/touch/resources/themes/stylesheets/sencha-touch/default",
|
209
202
|
]
|
203
|
+
|
204
|
+
@options += extract_jsb_build_files("#{@sdk_dir}/touch/sencha-touch.jsb3")
|
205
|
+
end
|
206
|
+
|
207
|
+
# Extracts files of first build in jsb file
|
208
|
+
def extract_jsb_build_files(jsb_file)
|
209
|
+
json = JSON.parse(IO.read(jsb_file))
|
210
|
+
basedir = File.dirname(jsb_file)
|
211
|
+
|
212
|
+
return json["builds"][0]["packages"].map do |package_id|
|
213
|
+
package = json["packages"].find {|p| p["id"] == package_id }
|
214
|
+
package["files"].map do |file|
|
215
|
+
basedir + "/" + file["path"] + file["name"]
|
216
|
+
end
|
217
|
+
end.flatten
|
210
218
|
end
|
211
219
|
|
212
220
|
# Returns shortened hash of naming current git revision
|
@@ -286,8 +294,8 @@ class JsDuckRunner
|
|
286
294
|
|
287
295
|
# Copy over the images that SDK documentation links to
|
288
296
|
def copy_sdk_images
|
289
|
-
system "cp -r #{@sdk_dir}/extjs/
|
290
|
-
system "cp -r #{@sdk_dir}/platform/
|
297
|
+
system "cp -r #{@sdk_dir}/extjs/docs/resources #{@out_dir}/doc-resources"
|
298
|
+
system "cp -r #{@sdk_dir}/platform/docs/resources/* #{@out_dir}/doc-resources"
|
291
299
|
end
|
292
300
|
|
293
301
|
# Copy over the images that Ext4 documentation links to
|
@@ -320,6 +328,12 @@ task :sass do
|
|
320
328
|
system "compass compile --quiet template/resources/sass"
|
321
329
|
end
|
322
330
|
|
331
|
+
desc "Updates JSB3 file for Docs app.\n"+
|
332
|
+
"Run this before every commit that changes JS dependencies."
|
333
|
+
task :jsb do
|
334
|
+
system("sencha", "create", "jsb", "-a", "template/build-js.html", "-p", "template/app.jsb3")
|
335
|
+
end
|
336
|
+
|
323
337
|
desc "Run JSDuck on Ext JS SDK (for internal use at Sencha)\n" +
|
324
338
|
"sdk - creates debug/development version\n" +
|
325
339
|
"sdk[export] - creates export version\n" +
|
@@ -333,7 +347,7 @@ task :sdk, [:mode] => :sass do |t, args|
|
|
333
347
|
runner.add_sdk
|
334
348
|
runner.add_debug if mode == "debug"
|
335
349
|
runner.add_seo if mode == "debug" || mode == "live"
|
336
|
-
runner.
|
350
|
+
runner.add_sdk_export_notice if mode == "export"
|
337
351
|
runner.add_google_analytics if mode == "live"
|
338
352
|
runner.run
|
339
353
|
|
data/bin/jsduck
CHANGED
data/js-classes/Array.js
ADDED
@@ -0,0 +1,561 @@
|
|
1
|
+
/**
|
2
|
+
* @class Array
|
3
|
+
*
|
4
|
+
* In JavaScript, the `Array` property of the global object is a constructor for
|
5
|
+
* array instances.
|
6
|
+
*
|
7
|
+
* An array is a JavaScript object. Note that you shouldn't use it as an
|
8
|
+
* associative array, use {@link Object} instead.
|
9
|
+
*
|
10
|
+
* # Creating an Array
|
11
|
+
*
|
12
|
+
* The following example creates an array, msgArray, with a length of 0, then assigns values to
|
13
|
+
* msgArray[0] and msgArray[99], changing the length of the array to 100.
|
14
|
+
*
|
15
|
+
* var msgArray = new Array();
|
16
|
+
* msgArray[0] = "Hello";
|
17
|
+
* msgArray[99] = "world";
|
18
|
+
*
|
19
|
+
* if (msgArray.length == 100)
|
20
|
+
* print("The length is 100.");
|
21
|
+
*
|
22
|
+
* # Creating a Two-dimensional Array
|
23
|
+
*
|
24
|
+
* The following creates chess board as a two dimensional array of strings. The first move is made by
|
25
|
+
* copying the 'P' in 6,4 to 4,4. The position 4,4 is left blank.
|
26
|
+
*
|
27
|
+
* var board =
|
28
|
+
* [ ['R','N','B','Q','K','B','N','R'],
|
29
|
+
* ['P','P','P','P','P','P','P','P'],
|
30
|
+
* [' ',' ',' ',' ',' ',' ',' ',' '],
|
31
|
+
* [' ',' ',' ',' ',' ',' ',' ',' '],
|
32
|
+
* [' ',' ',' ',' ',' ',' ',' ',' '],
|
33
|
+
* [' ',' ',' ',' ',' ',' ',' ',' '],
|
34
|
+
* ['p','p','p','p','p','p','p','p'],
|
35
|
+
* ['r','n','b','q','k','b','n','r']];
|
36
|
+
* print(board.join('\n') + '\n\n');
|
37
|
+
*
|
38
|
+
* // Move King's Pawn forward 2
|
39
|
+
* board[4][4] = board[6][4];
|
40
|
+
* board[6][4] = ' ';
|
41
|
+
* print(board.join('\n'));
|
42
|
+
*
|
43
|
+
* Here is the output:
|
44
|
+
*
|
45
|
+
* R,N,B,Q,K,B,N,R
|
46
|
+
* P,P,P,P,P,P,P,P
|
47
|
+
* , , , , , , ,
|
48
|
+
* , , , , , , ,
|
49
|
+
* , , , , , , ,
|
50
|
+
* , , , , , , ,
|
51
|
+
* p,p,p,p,p,p,p,p
|
52
|
+
* r,n,b,q,k,b,n,r
|
53
|
+
*
|
54
|
+
* R,N,B,Q,K,B,N,R
|
55
|
+
* P,P,P,P,P,P,P,P
|
56
|
+
* , , , , , , ,
|
57
|
+
* , , , , , , ,
|
58
|
+
* , , , ,p, , ,
|
59
|
+
* , , , , , , ,
|
60
|
+
* p,p,p,p, ,p,p,p
|
61
|
+
* r,n,b,q,k,b,n,r
|
62
|
+
*
|
63
|
+
* # Accessing array elements
|
64
|
+
*
|
65
|
+
* Array elements are nothing less than object properties, so they are accessed as such.
|
66
|
+
*
|
67
|
+
* var myArray = new Array("Wind", "Rain", "Fire");
|
68
|
+
* myArray[0]; // "Wind"
|
69
|
+
* myArray[1]; // "Rain"
|
70
|
+
* // etc.
|
71
|
+
* myArray.length; // 3
|
72
|
+
*
|
73
|
+
* // Even if indices are properties, the following notation throws a syntax error
|
74
|
+
* myArray.2;
|
75
|
+
*
|
76
|
+
* // It should be noted that in JavaScript, object property names are strings. Consequently,
|
77
|
+
* myArray[0] === myArray["0"];
|
78
|
+
* myArray[1] === myArray["1"];
|
79
|
+
* // etc.
|
80
|
+
*
|
81
|
+
* // However, this should be considered carefully
|
82
|
+
* myArray[02]; // "Fire". The number 02 is converted as the "2" string
|
83
|
+
* myArray["02"]; // undefined. There is no property named "02"
|
84
|
+
*
|
85
|
+
* # Relationship between length and numerical properties
|
86
|
+
*
|
87
|
+
* An array's length property and numerical properties are connected. Here is some
|
88
|
+
* code explaining how this relationship works.
|
89
|
+
*
|
90
|
+
* var a = [];
|
91
|
+
*
|
92
|
+
* a[0] = 'a';
|
93
|
+
* console.log(a[0]); // 'a'
|
94
|
+
* console.log(a.length); // 1
|
95
|
+
*
|
96
|
+
* a[1] = 32;
|
97
|
+
* console.log(a[1]); // 32
|
98
|
+
* console.log(a.length); // 2
|
99
|
+
*
|
100
|
+
* a[13] = 12345;
|
101
|
+
* console.log(a[13]); // 12345
|
102
|
+
* console.log(a.length); // 14
|
103
|
+
*
|
104
|
+
* a.length = 10;
|
105
|
+
* console.log(a[13]); // undefined, when reducing the length elements after length+1 are removed
|
106
|
+
* console.log(a.length); // 10
|
107
|
+
*
|
108
|
+
* # Creating an array using the result of a match
|
109
|
+
*
|
110
|
+
* The result of a match between a regular expression and a string can create an array.
|
111
|
+
* This array has properties and elements that provide information about the match. An
|
112
|
+
* array is the return value of RegExp.exec, String.match, and String.replace. To help
|
113
|
+
* explain these properties and elements, look at the following example and then refer
|
114
|
+
* to the table below:
|
115
|
+
*
|
116
|
+
* // Match one d followed by one or more b's followed by one d
|
117
|
+
* // Remember matched b's and the following d
|
118
|
+
* // Ignore case
|
119
|
+
*
|
120
|
+
* var myRe = /d(b+)(d)/i;
|
121
|
+
* var myArray = myRe.exec("cdbBdbsbz");
|
122
|
+
*
|
123
|
+
* The properties and elements returned from this match are as follows:
|
124
|
+
*
|
125
|
+
*
|
126
|
+
* | Property/Element | Description | Example
|
127
|
+
* |:-----------------|:--------------------------------------------------------------------------------------|:-------------------
|
128
|
+
* | `input` | A read-only property that reflects the original string against which the | cdbBdbsbz
|
129
|
+
* | | regular expression was matched. |
|
130
|
+
* | `index` | A read-only property that is the zero-based index of the match in the string. | 1
|
131
|
+
* | `[0]` | A read-only element that specifies the last matched characters. | dbBd
|
132
|
+
* | `[1], ...[n]` | Read-only elements that specify the parenthesized substring matches, if included in | [1]: bB [2]: d
|
133
|
+
* | | the regular expression. The number of possible parenthesized substrings is unlimited. |
|
134
|
+
*
|
135
|
+
* <div class="notice">
|
136
|
+
* Documentation for this class comes from <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array">MDN</a>
|
137
|
+
* and is available under <a href="http://creativecommons.org/licenses/by-sa/2.0/">Creative Commons: Attribution-Sharealike license</a>.
|
138
|
+
* </div>
|
139
|
+
*
|
140
|
+
*/
|
141
|
+
|
142
|
+
/**
|
143
|
+
* @method constructor
|
144
|
+
* Creates new Array object.
|
145
|
+
*
|
146
|
+
* @param {Number/Object...} items Either a number that specifies the length of array or any number of items
|
147
|
+
* for the array.
|
148
|
+
*/
|
149
|
+
|
150
|
+
// Properties
|
151
|
+
|
152
|
+
/**
|
153
|
+
* @property {Number} length
|
154
|
+
* Reflects the number of elements in an array.
|
155
|
+
*
|
156
|
+
* The value of the `length` property is an integer with a positive sign and a value less than 2 to the 32
|
157
|
+
* power (232).
|
158
|
+
*
|
159
|
+
* You can set the `length` property to truncate an array at any time. When you extend an array by changing
|
160
|
+
* its `length` property, the number of actual elements does not increase; for example, if you set `length`
|
161
|
+
* to 3 when it is currently 2, the array still contains only 2 elements.
|
162
|
+
*
|
163
|
+
* In the following example the array numbers is iterated through by looking at the `length` property to see
|
164
|
+
* how many elements it has. Each value is then doubled.
|
165
|
+
*
|
166
|
+
* var numbers = [1,2,3,4,5];
|
167
|
+
* for (var i = 0; i < numbers.length; i++) {
|
168
|
+
* numbers[i] *= 2;
|
169
|
+
* }
|
170
|
+
* // numbers is now [2,4,6,8,10];
|
171
|
+
*
|
172
|
+
* The following example shortens the array `statesUS` to a length of 50 if the current `length` is greater
|
173
|
+
* than 50.
|
174
|
+
*
|
175
|
+
* if (statesUS.length > 50) {
|
176
|
+
* statesUS.length=50
|
177
|
+
* }
|
178
|
+
*/
|
179
|
+
|
180
|
+
// Mutator methods. These methods modify the array:
|
181
|
+
|
182
|
+
/**
|
183
|
+
* @method pop
|
184
|
+
* The pop method removes the last element from an array and returns that value to the caller.
|
185
|
+
*
|
186
|
+
* `pop` is intentionally generic; this method can be called or applied to objects resembling
|
187
|
+
* arrays. Objects which do not contain a length property reflecting the last in a series of
|
188
|
+
* consecutive, zero-based numerical properties may not behave in any meaningful manner.
|
189
|
+
*
|
190
|
+
* var myFish = ["angel", "clown", "mandarin", "surgeon"];
|
191
|
+
* var popped = myFish.pop();
|
192
|
+
* alert(popped); // Alerts 'surgeon'
|
193
|
+
*
|
194
|
+
* @return {Object} The last element in the array
|
195
|
+
*/
|
196
|
+
|
197
|
+
/**
|
198
|
+
* @method push
|
199
|
+
* Adds one or more elements to the end of an array and returns the new length of the array.
|
200
|
+
*
|
201
|
+
* `push` is intentionally generic. This method can be called or applied to objects resembling
|
202
|
+
* arrays. The push method relies on a length property to determine where to start inserting
|
203
|
+
* the given values. If the length property cannot be converted into a number, the index used
|
204
|
+
* is 0. This includes the possibility of length being nonexistent, in which case length will
|
205
|
+
* also be created.
|
206
|
+
*
|
207
|
+
* The only native, array-like objects are strings, although they are not suitable in
|
208
|
+
* applications of this method, as strings are immutable.
|
209
|
+
*
|
210
|
+
* ### Adding elements to an array
|
211
|
+
*
|
212
|
+
* The following code creates the sports array containing two elements, then appends two elements
|
213
|
+
* to it. After the code executes, sports contains 4 elements: "soccer", "baseball", "football"
|
214
|
+
* and "swimming".
|
215
|
+
*
|
216
|
+
* var sports = ["soccer", "baseball"];
|
217
|
+
* sports.push("football", "swimming");
|
218
|
+
*
|
219
|
+
* @param {Object...} elements The elements to add to the end of the array.
|
220
|
+
* @return {Number} The new length property of the object upon which the method was called.
|
221
|
+
*/
|
222
|
+
|
223
|
+
/**
|
224
|
+
* @method reverse
|
225
|
+
* Reverses the order of the elements of an array -- the first becomes the last, and the
|
226
|
+
* last becomes the first.
|
227
|
+
*
|
228
|
+
* The reverse method transposes the elements of the calling array object in place, mutating the
|
229
|
+
* array, and returning a reference to the array.
|
230
|
+
*
|
231
|
+
* The following example creates an array myArray, containing three elements, then reverses the array.
|
232
|
+
*
|
233
|
+
* var myArray = ["one", "two", "three"];
|
234
|
+
* myArray.reverse();
|
235
|
+
*
|
236
|
+
* This code changes myArray so that:
|
237
|
+
*
|
238
|
+
* - myArray[0] is "three"
|
239
|
+
* - myArray[1] is "two"
|
240
|
+
* - myArray[2] is "one"
|
241
|
+
*
|
242
|
+
* @return {Array} A reference to the array
|
243
|
+
*/
|
244
|
+
|
245
|
+
/**
|
246
|
+
* @method shift
|
247
|
+
* Removes the first element from an array and returns that element.
|
248
|
+
*
|
249
|
+
* The `shift` method removes the element at the zeroeth index and shifts the values at consecutive
|
250
|
+
* indexes down, then returns the removed value.
|
251
|
+
*
|
252
|
+
* `shift` is intentionally generic; this method can be called or applied to objects resembling
|
253
|
+
* arrays. Objects which do not contain a `length` property reflecting the last in a series of
|
254
|
+
* consecutive, zero-based numerical properties may not behave in any meaningful manner.
|
255
|
+
*
|
256
|
+
* The following code displays the `myFish` array before and after removing its first element. It also
|
257
|
+
* displays the removed element:
|
258
|
+
*
|
259
|
+
* // assumes a println function is defined
|
260
|
+
* var myFish = ["angel", "clown", "mandarin", "surgeon"];
|
261
|
+
* println("myFish before: " + myFish);
|
262
|
+
* var shifted = myFish.shift();
|
263
|
+
* println("myFish after: " + myFish);
|
264
|
+
* println("Removed this element: " + shifted);
|
265
|
+
*
|
266
|
+
* This example displays the following:
|
267
|
+
*
|
268
|
+
* myFish before: angel,clown,mandarin,surgeon
|
269
|
+
* myFish after: clown,mandarin,surgeon
|
270
|
+
* Removed this element: angel
|
271
|
+
*
|
272
|
+
* @return {Object} The first element of the array prior to shifting.
|
273
|
+
*/
|
274
|
+
|
275
|
+
/**
|
276
|
+
* @method sort
|
277
|
+
* Sorts the elements of an array.
|
278
|
+
*
|
279
|
+
* If `compareFunction` is not supplied, elements are sorted by converting them to strings and
|
280
|
+
* comparing strings in lexicographic ("dictionary" or "telephone book," not numerical) order. For
|
281
|
+
* example, "80" comes before "9" in lexicographic order, but in a numeric sort 9 comes before 80.
|
282
|
+
*
|
283
|
+
* If `compareFunction` is supplied, the array elements are sorted according to the return value of
|
284
|
+
* the compare function. If a and b are two elements being compared, then:
|
285
|
+
* If `compareFunction(a, b)` is less than 0, sort `a` to a lower index than `b`.
|
286
|
+
* If `compareFunction(a, b)` returns 0, leave `a` and `b` unchanged with respect to each other, but
|
287
|
+
* sorted with respect to all different elements. Note: the ECMAscript standard does not guarantee
|
288
|
+
* this behaviour, and thus not all browsers respect this.
|
289
|
+
* If `compareFunction(a, b)` is greater than 0, sort `b` to a lower index than `a`.
|
290
|
+
* `compareFunction(a, b)` must always returns the same value when given a specific pair of elements a
|
291
|
+
* and b as its two arguments. If inconsistent results are returned then the sort order is undefined
|
292
|
+
*
|
293
|
+
* So, the compare function has the following form:
|
294
|
+
*
|
295
|
+
* function compare(a, b)
|
296
|
+
* {
|
297
|
+
* if (a is less than b by some ordering criterion)
|
298
|
+
* return -1;
|
299
|
+
* if (a is greater than b by the ordering criterion)
|
300
|
+
* return 1;
|
301
|
+
* // a must be equal to b
|
302
|
+
* return 0;
|
303
|
+
* }
|
304
|
+
*
|
305
|
+
* To compare numbers instead of strings, the compare function can simply subtract `b` from `a`:
|
306
|
+
*
|
307
|
+
* function compareNumbers(a, b)
|
308
|
+
* {
|
309
|
+
* return a - b;
|
310
|
+
* }
|
311
|
+
*
|
312
|
+
* The sort() method can be conveniently used with closures:
|
313
|
+
*
|
314
|
+
* var numbers = [4, 2, 5, 1, 3];
|
315
|
+
* numbers.sort(function(a, b) {
|
316
|
+
* return a - b;
|
317
|
+
* });
|
318
|
+
* print(numbers);
|
319
|
+
*
|
320
|
+
* @param {Function} compareFunction Specifies a function that defines the sort order. If omitted, the
|
321
|
+
* array is sorted lexicographically (in dictionary order) according to the string conversion of each
|
322
|
+
* element.
|
323
|
+
* @return {Array} A reference to the array
|
324
|
+
*/
|
325
|
+
|
326
|
+
/**
|
327
|
+
* @method splice
|
328
|
+
* Adds and/or removes elements from an array.
|
329
|
+
*
|
330
|
+
* If you specify a different number of elements to insert than the number you're removing, the array
|
331
|
+
* will have a different length at the end of the call.
|
332
|
+
*
|
333
|
+
* // assumes a print function is defined
|
334
|
+
* var myFish = ["angel", "clown", "mandarin", "surgeon"];
|
335
|
+
* print("myFish: " + myFish);
|
336
|
+
*
|
337
|
+
* var removed = myFish.splice(2, 0, "drum");
|
338
|
+
* print("After adding 1: " + myFish);
|
339
|
+
* print("removed is: " + removed);
|
340
|
+
*
|
341
|
+
* removed = myFish.splice(3, 1);
|
342
|
+
* print("After removing 1: " + myFish);
|
343
|
+
* print("removed is: " + removed);
|
344
|
+
*
|
345
|
+
* removed = myFish.splice(2, 1, "trumpet");
|
346
|
+
* print("After replacing 1: " + myFish);
|
347
|
+
* print("removed is: " + removed);
|
348
|
+
*
|
349
|
+
* removed = myFish.splice(0, 2, "parrot", "anemone", "blue");
|
350
|
+
* print("After replacing 2: " + myFish);
|
351
|
+
* print("removed is: " + removed);
|
352
|
+
*
|
353
|
+
* This script displays:
|
354
|
+
*
|
355
|
+
* myFish: angel,clown,mandarin,surgeon
|
356
|
+
* After adding 1: angel,clown,drum,mandarin,surgeon
|
357
|
+
* removed is:
|
358
|
+
* After removing 1: angel,clown,drum,surgeon
|
359
|
+
* removed is: mandarin
|
360
|
+
* After replacing 1: angel,clown,trumpet,surgeon
|
361
|
+
* removed is: drum
|
362
|
+
* After replacing 2: parrot,anemone,blue,trumpet,surgeon
|
363
|
+
* removed is: angel,clown
|
364
|
+
*
|
365
|
+
* @param {Number} index Index at which to start changing the array. If negative, will begin that
|
366
|
+
* many elements from the end.
|
367
|
+
* @param {Number} howMany An integer indicating the number of old array elements to remove. If
|
368
|
+
* `howMany` is 0, no elements are removed. In this case, you should specify at least one new element.
|
369
|
+
* If no `howMany` parameter is specified all elements after index are removed.
|
370
|
+
* @param {Object...} elements The elements to add to the array. If you don't specify any
|
371
|
+
* elements, `splice` simply removes elements from the array.
|
372
|
+
* @return {Array} An array containing the removed elements. If only one element is removed, an array
|
373
|
+
* of one element is returned..
|
374
|
+
*/
|
375
|
+
|
376
|
+
/**
|
377
|
+
* @method unshift
|
378
|
+
* Adds one or more elements to the front of an array and returns the new length of the array.
|
379
|
+
*
|
380
|
+
* The `unshift` method inserts the given values to the beginning of an array-like object.
|
381
|
+
*
|
382
|
+
* `unshift` is intentionally generic; this method can be called or applied to objects resembling
|
383
|
+
* arrays. Objects which do not contain a `length` property reflecting the last in a series of
|
384
|
+
* consecutive, zero-based numerical properties may not behave in any meaningful manner.
|
385
|
+
*
|
386
|
+
* The following code displays the myFish array before and after adding elements to it.
|
387
|
+
*
|
388
|
+
* // assumes a println function exists
|
389
|
+
* myFish = ["angel", "clown"];
|
390
|
+
* println("myFish before: " + myFish);
|
391
|
+
* unshifted = myFish.unshift("drum", "lion");
|
392
|
+
* println("myFish after: " + myFish);
|
393
|
+
* println("New length: " + unshifted);
|
394
|
+
*
|
395
|
+
* This example displays the following:
|
396
|
+
*
|
397
|
+
* myFish before: ["angel", "clown"]
|
398
|
+
* myFish after: ["drum", "lion", "angel", "clown"]
|
399
|
+
* New length: 4
|
400
|
+
*
|
401
|
+
* @param {Object...} elements The elements to add to the front of the array.
|
402
|
+
* @return {Number} The array's new length.
|
403
|
+
*/
|
404
|
+
|
405
|
+
// Accessor methods. These methods do not modify the array and return some representation of the array.
|
406
|
+
|
407
|
+
/**
|
408
|
+
* @method concat
|
409
|
+
* Returns a new array comprised of this array joined with other array(s) and/or value(s).
|
410
|
+
*
|
411
|
+
* `concat` creates a new array consisting of the elements in the `this` object on which it is called,
|
412
|
+
* followed in order by, for each argument, the elements of that argument (if the argument is an
|
413
|
+
* array) or the argument itself (if the argument is not an array).
|
414
|
+
*
|
415
|
+
* `concat` does not alter `this` or any of the arrays provided as arguments but instead returns a
|
416
|
+
* "one level deep" copy that contains copies of the same elements combined from the original arrays.
|
417
|
+
* Elements of the original arrays are copied into the new array as follows:
|
418
|
+
* Object references (and not the actual object): `concat` copies object references into the new
|
419
|
+
* array. Both the original and new array refer to the same object. That is, if a referenced object is
|
420
|
+
* modified, the changes are visible to both the new and original arrays.
|
421
|
+
* Strings and numbers (not {@link String} and {@link Number} objects): `concat` copies the values of
|
422
|
+
* strings and numbers into the new array.
|
423
|
+
*
|
424
|
+
* Any operation on the new array will have no effect on the original arrays, and vice versa.
|
425
|
+
*
|
426
|
+
* ### Concatenating two arrays
|
427
|
+
*
|
428
|
+
* The following code concatenates two arrays:
|
429
|
+
*
|
430
|
+
* var alpha = ["a", "b", "c"];
|
431
|
+
* var numeric = [1, 2, 3];
|
432
|
+
*
|
433
|
+
* // creates array ["a", "b", "c", 1, 2, 3]; alpha and numeric are unchanged
|
434
|
+
* var alphaNumeric = alpha.concat(numeric);
|
435
|
+
*
|
436
|
+
* ### Concatenating three arrays
|
437
|
+
*
|
438
|
+
* The following code concatenates three arrays:
|
439
|
+
*
|
440
|
+
* var num1 = [1, 2, 3];
|
441
|
+
* var num2 = [4, 5, 6];
|
442
|
+
* var num3 = [7, 8, 9];
|
443
|
+
*
|
444
|
+
* // creates array [1, 2, 3, 4, 5, 6, 7, 8, 9]; num1, num2, num3 are unchanged
|
445
|
+
* var nums = num1.concat(num2, num3);
|
446
|
+
*
|
447
|
+
* ### Concatenating values to an array
|
448
|
+
*
|
449
|
+
* The following code concatenates three values to an array:
|
450
|
+
*
|
451
|
+
* var alpha = ['a', 'b', 'c'];
|
452
|
+
*
|
453
|
+
* // creates array ["a", "b", "c", 1, 2, 3], leaving alpha unchanged
|
454
|
+
* var alphaNumeric = alpha.concat(1, [2, 3]);
|
455
|
+
*
|
456
|
+
* @param {Object...} values Arrays and/or values to concatenate to the resulting array.
|
457
|
+
* @return {Array} New array.
|
458
|
+
*/
|
459
|
+
|
460
|
+
/**
|
461
|
+
* @method join
|
462
|
+
* Joins all elements of an array into a string.
|
463
|
+
*
|
464
|
+
* The string conversions of all array elements are joined into one string.
|
465
|
+
*
|
466
|
+
* The following example creates an array, `a`, with three elements, then joins the array three times:
|
467
|
+
* using the default separator, then a comma and a space, and then a plus.
|
468
|
+
*
|
469
|
+
* var a = new Array("Wind","Rain","Fire");
|
470
|
+
* var myVar1 = a.join(); // assigns "Wind,Rain,Fire" to myVar1
|
471
|
+
* var myVar2 = a.join(", "); // assigns "Wind, Rain, Fire" to myVar2
|
472
|
+
* var myVar3 = a.join(" + "); // assigns "Wind + Rain + Fire" to myVar3
|
473
|
+
*
|
474
|
+
* @param {String} separator Specifies a string to separate each element of the array. The separator
|
475
|
+
* is converted to a string if necessary. If omitted, the array elements are separated with a comma.
|
476
|
+
* @return {String} A string of the array elements.
|
477
|
+
*/
|
478
|
+
|
479
|
+
/**
|
480
|
+
* @method slice
|
481
|
+
* Extracts a section of an array and returns a new array.
|
482
|
+
*
|
483
|
+
* `slice` does not alter the original array, but returns a new "one level deep" copy that contains
|
484
|
+
* copies of the elements sliced from the original array. Elements of the original array are copied
|
485
|
+
* into the new array as follows:
|
486
|
+
* * For object references (and not the actual object), `slice` copies object references into the
|
487
|
+
* new array. Both the original and new array refer to the same object. If a referenced object
|
488
|
+
* changes, the changes are visible to both the new and original arrays.
|
489
|
+
* * For strings and numbers (not {@link String} and {@link Number} objects), `slice` copies strings
|
490
|
+
* and numbers into the new array. Changes to the string or number in one array does not affect the
|
491
|
+
* other array.
|
492
|
+
*
|
493
|
+
* If a new element is added to either array, the other array is not affected.
|
494
|
+
*
|
495
|
+
* ### Using slice
|
496
|
+
*
|
497
|
+
* In the following example, `slice` creates a new array, `newCar`, from `myCar`. Both include a
|
498
|
+
* reference to the object `myHonda`. When the color of `myHonda` is changed to purple, both arrays
|
499
|
+
* reflect the change.
|
500
|
+
*
|
501
|
+
* // Using slice, create newCar from myCar.
|
502
|
+
* var myHonda = { color: "red", wheels: 4, engine: { cylinders: 4, size: 2.2 } };
|
503
|
+
* var myCar = [myHonda, 2, "cherry condition", "purchased 1997"];
|
504
|
+
* var newCar = myCar.slice(0, 2);
|
505
|
+
*
|
506
|
+
* // Print the values of myCar, newCar, and the color of myHonda
|
507
|
+
* // referenced from both arrays.
|
508
|
+
* print("myCar = " + myCar.toSource());
|
509
|
+
* print("newCar = " + newCar.toSource());
|
510
|
+
* print("myCar[0].color = " + myCar[0].color);
|
511
|
+
* print("newCar[0].color = " + newCar[0].color);
|
512
|
+
*
|
513
|
+
* // Change the color of myHonda.
|
514
|
+
* myHonda.color = "purple";
|
515
|
+
* print("The new color of my Honda is " + myHonda.color);
|
516
|
+
*
|
517
|
+
* // Print the color of myHonda referenced from both arrays.
|
518
|
+
* print("myCar[0].color = " + myCar[0].color);
|
519
|
+
* print("newCar[0].color = " + newCar[0].color);
|
520
|
+
*
|
521
|
+
* This script writes:
|
522
|
+
*
|
523
|
+
* myCar = [{color:"red", wheels:4, engine:{cylinders:4, size:2.2}}, 2, "cherry condition",
|
524
|
+
* "purchased 1997"]
|
525
|
+
* newCar = [{color:"red", wheels:4, engine:{cylinders:4, size:2.2}}, 2]
|
526
|
+
* myCar[0].color = red
|
527
|
+
* newCar[0].color = red
|
528
|
+
* The new color of my Honda is purple
|
529
|
+
* myCar[0].color = purple
|
530
|
+
* newCar[0].color = purple
|
531
|
+
*
|
532
|
+
* @param {Number} begin Zero-based index at which to begin extraction.
|
533
|
+
* As a negative index, `start` indicates an offset from the end of the sequence. `slice(-2)` extracts
|
534
|
+
* the second-to-last element and the last element in the sequence
|
535
|
+
* @param {Number} end Zero-based index at which to end extraction. `slice` extracts up to but not
|
536
|
+
* including `end`.
|
537
|
+
* `slice(1,4)` extracts the second element through the fourth element (elements indexed 1, 2, and 3).
|
538
|
+
* As a negative index, end indicates an offset from the end of the sequence. `slice(2,-1)` extracts
|
539
|
+
* the third element through the second-to-last element in the sequence.
|
540
|
+
* If `end` is omitted, `slice` extracts to the end of the sequence.
|
541
|
+
* @return {Array} Array from the new start position up to (but not including) the specified end position.
|
542
|
+
*/
|
543
|
+
|
544
|
+
/**
|
545
|
+
* @method toString
|
546
|
+
* Returns a string representing the array and its elements. Overrides the `Object.prototype.toString`
|
547
|
+
* method.
|
548
|
+
*
|
549
|
+
* The {@link Array} object overrides the `toString` method of {@link Object}. For Array objects, the
|
550
|
+
* `toString` method joins the array and returns one string containing each array element separated by
|
551
|
+
* commas. For example, the following code creates an array and uses `toString` to convert the array
|
552
|
+
* to a string.
|
553
|
+
*
|
554
|
+
* var monthNames = new Array("Jan","Feb","Mar","Apr");
|
555
|
+
* myVar = monthNames.toString(); // assigns "Jan,Feb,Mar,Apr" to myVar
|
556
|
+
*
|
557
|
+
* JavaScript calls the `toString` method automatically when an array is to be represented as a text
|
558
|
+
* value or when an array is referred to in a string concatenation.
|
559
|
+
*
|
560
|
+
* @return {String} The array as a string.
|
561
|
+
*/
|