X-Git-Url: http://git.ithinksw.org/extjs.git/blobdiff_plain/6746dc89c47ed01b165cc1152533605f97eb8e8d..f562e4c6e5fac7bcb445985b99acbea4d706e6f0:/docs/output/Array.js diff --git a/docs/output/Array.js b/docs/output/Array.js new file mode 100644 index 00000000..06358a04 --- /dev/null +++ b/docs/output/Array.js @@ -0,0 +1 @@ +Ext.data.JsonP.Array({"tagname":"class","html":"

Files

In JavaScript, the Array property of the global object is a constructor for\narray instances.

\n\n

An array is a JavaScript object. Note that you shouldn't use it as an\nassociative array, use Object instead.

\n\n

Creating an Array

\n\n

The following example creates an array, msgArray, with a length of 0, then assigns values to\nmsgArray[0] and msgArray[99], changing the length of the array to 100.

\n\n
var msgArray = new Array();\nmsgArray[0] = \"Hello\";\nmsgArray[99] = \"world\";\n\nif (msgArray.length == 100)\nprint(\"The length is 100.\");\n
\n\n

Creating a Two-dimensional Array

\n\n

The following creates chess board as a two dimensional array of strings. The first move is made by\ncopying the 'P' in 6,4 to 4,4. The position 4,4 is left blank.

\n\n
var board =\n[ ['R','N','B','Q','K','B','N','R'],\n['P','P','P','P','P','P','P','P'],\n[' ',' ',' ',' ',' ',' ',' ',' '],\n[' ',' ',' ',' ',' ',' ',' ',' '],\n[' ',' ',' ',' ',' ',' ',' ',' '],\n[' ',' ',' ',' ',' ',' ',' ',' '],\n['p','p','p','p','p','p','p','p'],\n['r','n','b','q','k','b','n','r']];\nprint(board.join('\\n') + '\\n\\n');\n\n// Move King's Pawn forward 2\nboard[4][4] = board[6][4];\nboard[6][4] = ' ';\nprint(board.join('\\n'));\n
\n\n

Here is the output:

\n\n
R,N,B,Q,K,B,N,R\nP,P,P,P,P,P,P,P\n , , , , , , ,\n , , , , , , ,\n , , , , , , ,\n , , , , , , ,\np,p,p,p,p,p,p,p\nr,n,b,q,k,b,n,r\n\nR,N,B,Q,K,B,N,R\nP,P,P,P,P,P,P,P\n , , , , , , ,\n , , , , , , ,\n , , , ,p, , ,\n , , , , , , ,\np,p,p,p, ,p,p,p\nr,n,b,q,k,b,n,r\n
\n\n

Accessing array elements

\n\n

Array elements are nothing less than object properties, so they are accessed as such.

\n\n
var myArray = new Array(\"Wind\", \"Rain\", \"Fire\");\nmyArray[0]; // \"Wind\"\nmyArray[1]; // \"Rain\"\n// etc.\nmyArray.length; // 3\n\n// Even if indices are properties, the following notation throws a syntax error\nmyArray.2;\n\n// It should be noted that in JavaScript, object property names are strings. Consequently,\nmyArray[0] === myArray[\"0\"];\nmyArray[1] === myArray[\"1\"];\n// etc.\n\n// However, this should be considered carefully\nmyArray[02]; // \"Fire\". The number 02 is converted as the \"2\" string\nmyArray[\"02\"]; // undefined. There is no property named \"02\"\n
\n\n

Relationship between length and numerical properties

\n\n

An array's length property and numerical properties are connected. Here is some\ncode explaining how this relationship works.

\n\n
var a = [];\n\na[0] = 'a';\nconsole.log(a[0]); // 'a'\nconsole.log(a.length); // 1\n\na[1] = 32;\nconsole.log(a[1]); // 32\nconsole.log(a.length); // 2\n\na[13] = 12345;\nconsole.log(a[13]); // 12345\nconsole.log(a.length); // 14\n\na.length = 10;\nconsole.log(a[13]); // undefined, when reducing the length elements after length+1 are removed\nconsole.log(a.length); // 10\n
\n\n

Creating an array using the result of a match

\n\n

The result of a match between a regular expression and a string can create an array.\nThis array has properties and elements that provide information about the match. An\narray is the return value of RegExp.exec, String.match, and String.replace. To help\nexplain these properties and elements, look at the following example and then refer\nto the table below:

\n\n
// Match one d followed by one or more b's followed by one d\n// Remember matched b's and the following d\n// Ignore case\n\nvar myRe = /d(b+)(d)/i;\nvar myArray = myRe.exec(\"cdbBdbsbz\");\n
\n\n

The properties and elements returned from this match are as follows:

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Property/Element Description Example
input A read-only property that reflects the original string against which the cdbBdbsbz
regular expression was matched.
index A read-only property that is the zero-based index of the match in the string. 1
[0] A read-only element that specifies the last matched characters. dbBd
[1], ...[n] Read-only elements that specify the parenthesized substring matches, if included in [1]: bB [2]: d
the regular expression. The number of possible parenthesized substrings is unlimited.
\n\n\n
\nDocumentation for this class comes from MDN\nand is available under Creative Commons: Attribution-Sharealike license.\n
\n\n
Defined By

Properties

Reflects the number of elements in an array. ...

Reflects the number of elements in an array.

\n\n

The value of the length property is an integer with a positive sign and a value less than 2 to the 32\npower (232).

\n\n

You can set the length property to truncate an array at any time. When you extend an array by changing\nits length property, the number of actual elements does not increase; for example, if you set length\nto 3 when it is currently 2, the array still contains only 2 elements.

\n\n

In the following example the array numbers is iterated through by looking at the length property to see\nhow many elements it has. Each value is then doubled.

\n\n
var numbers = [1,2,3,4,5];\nfor (var i = 0; i < numbers.length; i++) {\n    numbers[i] *= 2;\n}\n// numbers is now [2,4,6,8,10];\n
\n\n

The following example shortens the array statesUS to a length of 50 if the current length is greater\nthan 50.

\n\n
if (statesUS.length > 50) {\n    statesUS.length=50\n}\n
\n
Defined By

Methods

Creates new Array object. ...

Creates new Array object.

\n

Parameters

  • items : Number/Object...

    Either a number that specifies the length of array or any number of items\nfor the array.

    \n

Returns

Returns a new array comprised of this array joined with other array(s) and/or value(s). ...

Returns a new array comprised of this array joined with other array(s) and/or value(s).

\n\n

concat creates a new array consisting of the elements in the this object on which it is called,\nfollowed in order by, for each argument, the elements of that argument (if the argument is an\narray) or the argument itself (if the argument is not an array).

\n\n

concat does not alter this or any of the arrays provided as arguments but instead returns a\n\"one level deep\" copy that contains copies of the same elements combined from the original arrays.\nElements of the original arrays are copied into the new array as follows:\nObject references (and not the actual object): concat copies object references into the new\narray. Both the original and new array refer to the same object. That is, if a referenced object is\nmodified, the changes are visible to both the new and original arrays.\nStrings and numbers (not String and Number objects): concat copies the values of\nstrings and numbers into the new array.

\n\n

Any operation on the new array will have no effect on the original arrays, and vice versa.

\n\n

Concatenating two arrays

\n\n

The following code concatenates two arrays:

\n\n
var alpha = [\"a\", \"b\", \"c\"];\nvar numeric = [1, 2, 3];\n\n// creates array [\"a\", \"b\", \"c\", 1, 2, 3]; alpha and numeric are unchanged\nvar alphaNumeric = alpha.concat(numeric);\n
\n\n

Concatenating three arrays

\n\n

The following code concatenates three arrays:

\n\n
var num1 = [1, 2, 3];\nvar num2 = [4, 5, 6];\nvar num3 = [7, 8, 9];\n\n// creates array [1, 2, 3, 4, 5, 6, 7, 8, 9]; num1, num2, num3 are unchanged\nvar nums = num1.concat(num2, num3);\n
\n\n

Concatenating values to an array

\n\n

The following code concatenates three values to an array:

\n\n
var alpha = ['a', 'b', 'c'];\n\n// creates array [\"a\", \"b\", \"c\", 1, 2, 3], leaving alpha unchanged\nvar alphaNumeric = alpha.concat(1, [2, 3]);\n
\n

Parameters

  • values : Object...

    Arrays and/or values to concatenate to the resulting array.

    \n

Returns

Joins all elements of an array into a string. ...

Joins all elements of an array into a string.

\n\n

The string conversions of all array elements are joined into one string.

\n\n

The following example creates an array, a, with three elements, then joins the array three times:\nusing the default separator, then a comma and a space, and then a plus.

\n\n
var a = new Array(\"Wind\",\"Rain\",\"Fire\");\nvar myVar1 = a.join();      // assigns \"Wind,Rain,Fire\" to myVar1\nvar myVar2 = a.join(\", \");  // assigns \"Wind, Rain, Fire\" to myVar2\nvar myVar3 = a.join(\" + \"); // assigns \"Wind + Rain + Fire\" to myVar3\n
\n

Parameters

  • separator : String

    Specifies a string to separate each element of the array. The separator\nis converted to a string if necessary. If omitted, the array elements are separated with a comma.

    \n

Returns

  • String

    A string of the array elements.

    \n
The pop method removes the last element from an array and returns that value to the caller. ...

The pop method removes the last element from an array and returns that value to the caller.

\n\n

pop is intentionally generic; this method can be called or applied to objects resembling\narrays. Objects which do not contain a length property reflecting the last in a series of\nconsecutive, zero-based numerical properties may not behave in any meaningful manner.

\n\n
var myFish = [\"angel\", \"clown\", \"mandarin\", \"surgeon\"];\nvar popped = myFish.pop();\nalert(popped); // Alerts 'surgeon'\n
\n

Returns

  • Object

    The last element in the array

    \n
Adds one or more elements to the end of an array and returns the new length of the array. ...

Adds one or more elements to the end of an array and returns the new length of the array.

\n\n

push is intentionally generic. This method can be called or applied to objects resembling\narrays. The push method relies on a length property to determine where to start inserting\nthe given values. If the length property cannot be converted into a number, the index used\nis 0. This includes the possibility of length being nonexistent, in which case length will\nalso be created.

\n\n

The only native, array-like objects are strings, although they are not suitable in\napplications of this method, as strings are immutable.

\n\n

Adding elements to an array

\n\n

The following code creates the sports array containing two elements, then appends two elements\nto it. After the code executes, sports contains 4 elements: \"soccer\", \"baseball\", \"football\"\nand \"swimming\".

\n\n
var sports = [\"soccer\", \"baseball\"];\nsports.push(\"football\", \"swimming\");\n
\n

Parameters

  • elements : Object...

    The elements to add to the end of the array.

    \n

Returns

  • Number

    The new length property of the object upon which the method was called.

    \n
Reverses the order of the elements of an array -- the first becomes the last, and the\nlast becomes the first. ...

Reverses the order of the elements of an array -- the first becomes the last, and the\nlast becomes the first.

\n\n

The reverse method transposes the elements of the calling array object in place, mutating the\narray, and returning a reference to the array.

\n\n

The following example creates an array myArray, containing three elements, then reverses the array.

\n\n
var myArray = [\"one\", \"two\", \"three\"];\nmyArray.reverse();\n
\n\n

This code changes myArray so that:

\n\n
    \n
  • myArray[0] is \"three\"
  • \n
  • myArray[1] is \"two\"
  • \n
  • myArray[2] is \"one\"
  • \n
\n\n

Returns

  • Array

    A reference to the array

    \n
Removes the first element from an array and returns that element. ...

Removes the first element from an array and returns that element.

\n\n

The shift method removes the element at the zeroeth index and shifts the values at consecutive\nindexes down, then returns the removed value.

\n\n

shift is intentionally generic; this method can be called or applied to objects resembling\narrays. Objects which do not contain a length property reflecting the last in a series of\nconsecutive, zero-based numerical properties may not behave in any meaningful manner.

\n\n

The following code displays the myFish array before and after removing its first element. It also\ndisplays the removed element:

\n\n
// assumes a println function is defined\nvar myFish = [\"angel\", \"clown\", \"mandarin\", \"surgeon\"];\nprintln(\"myFish before: \" + myFish);\nvar shifted = myFish.shift();\nprintln(\"myFish after: \" + myFish);\nprintln(\"Removed this element: \" + shifted);\n
\n\n

This example displays the following:

\n\n
myFish before: angel,clown,mandarin,surgeon\nmyFish after: clown,mandarin,surgeon\nRemoved this element: angel\n
\n

Returns

  • Object

    The first element of the array prior to shifting.

    \n
Extracts a section of an array and returns a new array. ...

Extracts a section of an array and returns a new array.

\n\n

slice does not alter the original array, but returns a new \"one level deep\" copy that contains\ncopies of the elements sliced from the original array. Elements of the original array are copied\ninto the new array as follows:\n* For object references (and not the actual object), slice copies object references into the\nnew array. Both the original and new array refer to the same object. If a referenced object\nchanges, the changes are visible to both the new and original arrays.\n* For strings and numbers (not String and Number objects), slice copies strings\nand numbers into the new array. Changes to the string or number in one array does not affect the\nother array.

\n\n

If a new element is added to either array, the other array is not affected.

\n\n

Using slice

\n\n

In the following example, slice creates a new array, newCar, from myCar. Both include a\nreference to the object myHonda. When the color of myHonda is changed to purple, both arrays\nreflect the change.

\n\n
// Using slice, create newCar from myCar.\nvar myHonda = { color: \"red\", wheels: 4, engine: { cylinders: 4, size: 2.2 } };\nvar myCar = [myHonda, 2, \"cherry condition\", \"purchased 1997\"];\nvar newCar = myCar.slice(0, 2);\n\n// Print the values of myCar, newCar, and the color of myHonda\n//  referenced from both arrays.\nprint(\"myCar = \" + myCar.toSource());\nprint(\"newCar = \" + newCar.toSource());\nprint(\"myCar[0].color = \" + myCar[0].color);\nprint(\"newCar[0].color = \" + newCar[0].color);\n\n// Change the color of myHonda.\nmyHonda.color = \"purple\";\nprint(\"The new color of my Honda is \" + myHonda.color);\n\n// Print the color of myHonda referenced from both arrays.\nprint(\"myCar[0].color = \" + myCar[0].color);\nprint(\"newCar[0].color = \" + newCar[0].color);\n
\n\n

This script writes:

\n\n
myCar = [{color:\"red\", wheels:4, engine:{cylinders:4, size:2.2}}, 2, \"cherry condition\",\n\"purchased 1997\"]\nnewCar = [{color:\"red\", wheels:4, engine:{cylinders:4, size:2.2}}, 2]\nmyCar[0].color = red\nnewCar[0].color = red\nThe new color of my Honda is purple\nmyCar[0].color = purple\nnewCar[0].color = purple\n
\n

Parameters

  • begin : Number

    Zero-based index at which to begin extraction.\nAs a negative index, start indicates an offset from the end of the sequence. slice(-2) extracts\nthe second-to-last element and the last element in the sequence

    \n
  • end : Number

    Zero-based index at which to end extraction. slice extracts up to but not\nincluding end.\nslice(1,4) extracts the second element through the fourth element (elements indexed 1, 2, and 3).\nAs a negative index, end indicates an offset from the end of the sequence. slice(2,-1) extracts\nthe third element through the second-to-last element in the sequence.\nIf end is omitted, slice extracts to the end of the sequence.

    \n

Returns

  • Array

    Array from the new start position up to (but not including) the specified end position.

    \n
( Function compareFunction ) : Array
Sorts the elements of an array. ...

Sorts the elements of an array.

\n\n

If compareFunction is not supplied, elements are sorted by converting them to strings and\ncomparing strings in lexicographic (\"dictionary\" or \"telephone book,\" not numerical) order. For\nexample, \"80\" comes before \"9\" in lexicographic order, but in a numeric sort 9 comes before 80.

\n\n

If compareFunction is supplied, the array elements are sorted according to the return value of\nthe compare function. If a and b are two elements being compared, then:\nIf compareFunction(a, b) is less than 0, sort a to a lower index than b.\nIf compareFunction(a, b) returns 0, leave a and b unchanged with respect to each other, but\nsorted with respect to all different elements. Note: the ECMAscript standard does not guarantee\nthis behaviour, and thus not all browsers respect this.\nIf compareFunction(a, b) is greater than 0, sort b to a lower index than a.\ncompareFunction(a, b) must always returns the same value when given a specific pair of elements a\nand b as its two arguments. If inconsistent results are returned then the sort order is undefined

\n\n

So, the compare function has the following form:

\n\n
function compare(a, b)\n{\n    if (a is less than b by some ordering criterion)\n        return -1;\n    if (a is greater than b by the ordering criterion)\n       return 1;\n    // a must be equal to b\n    return 0;\n}\n
\n\n

To compare numbers instead of strings, the compare function can simply subtract b from a:

\n\n
function compareNumbers(a, b)\n{\nreturn a - b;\n}\n
\n\n

The sort() method can be conveniently used with closures:

\n\n
var numbers = [4, 2, 5, 1, 3];\nnumbers.sort(function(a, b) {\n    return a - b;\n});\nprint(numbers);\n
\n

Parameters

  • compareFunction : Function

    Specifies a function that defines the sort order. If omitted, the\narray is sorted lexicographically (in dictionary order) according to the string conversion of each\nelement.

    \n

Returns

  • Array

    A reference to the array

    \n
( Number index, Number howMany, Object... elements ) : Array
Adds and/or removes elements from an array. ...

Adds and/or removes elements from an array.

\n\n

If you specify a different number of elements to insert than the number you're removing, the array\nwill have a different length at the end of the call.

\n\n
// assumes a print function is defined\nvar myFish = [\"angel\", \"clown\", \"mandarin\", \"surgeon\"];\nprint(\"myFish: \" + myFish);\n\nvar removed = myFish.splice(2, 0, \"drum\");\nprint(\"After adding 1: \" + myFish);\nprint(\"removed is: \" + removed);\n\nremoved = myFish.splice(3, 1);\nprint(\"After removing 1: \" + myFish);\nprint(\"removed is: \" + removed);\n\nremoved = myFish.splice(2, 1, \"trumpet\");\nprint(\"After replacing 1: \" + myFish);\nprint(\"removed is: \" + removed);\n\nremoved = myFish.splice(0, 2, \"parrot\", \"anemone\", \"blue\");\nprint(\"After replacing 2: \" + myFish);\nprint(\"removed is: \" + removed);\n
\n\n

This script displays:

\n\n
myFish: angel,clown,mandarin,surgeon\nAfter adding 1: angel,clown,drum,mandarin,surgeon\nremoved is:\nAfter removing 1: angel,clown,drum,surgeon\nremoved is: mandarin\nAfter replacing 1: angel,clown,trumpet,surgeon\nremoved is: drum\nAfter replacing 2: parrot,anemone,blue,trumpet,surgeon\nremoved is: angel,clown\n
\n

Parameters

  • index : Number

    Index at which to start changing the array. If negative, will begin that\nmany elements from the end.

    \n
  • howMany : Number

    An integer indicating the number of old array elements to remove. If\nhowMany is 0, no elements are removed. In this case, you should specify at least one new element.\nIf no howMany parameter is specified all elements after index are removed.

    \n
  • elements : Object...

    The elements to add to the array. If you don't specify any\nelements, splice simply removes elements from the array.

    \n

Returns

  • Array

    An array containing the removed elements. If only one element is removed, an array\nof one element is returned..

    \n
Returns a string representing the array and its elements. ...

Returns a string representing the array and its elements. Overrides the Object.prototype.toString\nmethod.

\n\n

The Array object overrides the toString method of Object. For Array objects, the\ntoString method joins the array and returns one string containing each array element separated by\ncommas. For example, the following code creates an array and uses toString to convert the array\nto a string.

\n\n
var monthNames = new Array(\"Jan\",\"Feb\",\"Mar\",\"Apr\");\nmyVar = monthNames.toString(); // assigns \"Jan,Feb,Mar,Apr\" to myVar\n
\n\n

JavaScript calls the toString method automatically when an array is to be represented as a text\nvalue or when an array is referred to in a string concatenation.

\n

Returns

  • String

    The array as a string.

    \n
Adds one or more elements to the front of an array and returns the new length of the array. ...

Adds one or more elements to the front of an array and returns the new length of the array.

\n\n

The unshift method inserts the given values to the beginning of an array-like object.

\n\n

unshift is intentionally generic; this method can be called or applied to objects resembling\narrays. Objects which do not contain a length property reflecting the last in a series of\nconsecutive, zero-based numerical properties may not behave in any meaningful manner.

\n\n

The following code displays the myFish array before and after adding elements to it.

\n\n
// assumes a println function exists\nmyFish = [\"angel\", \"clown\"];\nprintln(\"myFish before: \" + myFish);\nunshifted = myFish.unshift(\"drum\", \"lion\");\nprintln(\"myFish after: \" + myFish);\nprintln(\"New length: \" + unshifted);\n
\n\n

This example displays the following:

\n\n
myFish before: [\"angel\", \"clown\"]\nmyFish after: [\"drum\", \"lion\", \"angel\", \"clown\"]\nNew length: 4\n
\n

Parameters

  • elements : Object...

    The elements to add to the front of the array.

    \n

Returns

  • Number

    The array's new length.

    \n
","allMixins":[],"meta":{},"requires":[],"deprecated":null,"extends":null,"inheritable":false,"static":false,"superclasses":[],"singleton":false,"code_type":"nop","alias":null,"statics":{"property":[],"css_var":[],"css_mixin":[],"cfg":[],"method":[],"event":[]},"subclasses":[],"uses":[],"protected":false,"mixins":[],"members":{"property":[{"tagname":"property","deprecated":null,"static":false,"owner":"Array","template":null,"required":null,"protected":false,"name":"length","id":"property-length"}],"css_var":[],"css_mixin":[],"cfg":[],"method":[{"tagname":"method","deprecated":null,"static":false,"owner":"Array","template":false,"required":null,"protected":false,"name":"constructor","id":"method-constructor"},{"tagname":"method","deprecated":null,"static":false,"owner":"Array","template":false,"required":null,"protected":false,"name":"concat","id":"method-concat"},{"tagname":"method","deprecated":null,"static":false,"owner":"Array","template":false,"required":null,"protected":false,"name":"join","id":"method-join"},{"tagname":"method","deprecated":null,"static":false,"owner":"Array","template":false,"required":null,"protected":false,"name":"pop","id":"method-pop"},{"tagname":"method","deprecated":null,"static":false,"owner":"Array","template":false,"required":null,"protected":false,"name":"push","id":"method-push"},{"tagname":"method","deprecated":null,"static":false,"owner":"Array","template":false,"required":null,"protected":false,"name":"reverse","id":"method-reverse"},{"tagname":"method","deprecated":null,"static":false,"owner":"Array","template":false,"required":null,"protected":false,"name":"shift","id":"method-shift"},{"tagname":"method","deprecated":null,"static":false,"owner":"Array","template":false,"required":null,"protected":false,"name":"slice","id":"method-slice"},{"tagname":"method","deprecated":null,"static":false,"owner":"Array","template":false,"required":null,"protected":false,"name":"sort","id":"method-sort"},{"tagname":"method","deprecated":null,"static":false,"owner":"Array","template":false,"required":null,"protected":false,"name":"splice","id":"method-splice"},{"tagname":"method","deprecated":null,"static":false,"owner":"Array","template":false,"required":null,"protected":false,"name":"toString","id":"method-toString"},{"tagname":"method","deprecated":null,"static":false,"owner":"Array","template":false,"required":null,"protected":false,"name":"unshift","id":"method-unshift"}],"event":[]},"private":false,"component":false,"name":"Array","alternateClassNames":[],"id":"class-Array","mixedInto":[],"xtypes":{},"files":[{"href":"Array.html#Array","filename":"Array.js"}]}); \ No newline at end of file