SoFunction
Updated on 2025-04-10

Spoken js objects and arrays


/*
Arrays and Objects [Authoritative JavaScript Guide, 5th Edition]
*/

/*
Object: is an unordered property collection, each property has its own name and value */

/* Simple method to create an object, direct quantity of objects */
var obj = {};
var obj = {name: 'maxthon'};
var obj = {name: {}, text: []};

/* New operator can be used */
var a = new Array();
var d = new Date();
var r = new RegExp('javascript', 'i');
var o = new Object(); // var o = {};
/* Note: the new operator is followed by the constructor, so
typeof Array; // 'function'
typeof Object; // 'function'
Object is an instance of Function.
Function is a special object and an instance of Object.
*/

/* Object properties */
// Use . to access the value of the attribute.
// Note: You can also use [], and use attribute names (variables can be used, which is particularly useful).
var t = {};
= 'hello';
= {};
= 'rd';
= [];

var t = {
"text": "hello"
};
(); // 'hello';
// Addition: The keyword var is usually used to declare variables, but when declaring object properties, the declaration of var cannot be used


/* Object Enumeration */

var F = function () {};
= 'RD';
var obj = new F;
for (var key in obj) {
(key); // name;
}

// Only enumerate the object itself, do not check up along the prototype chain
for (var key in obj) {
if ((key)) {
(key); //
}
}
/* Note: for in cannot enumerate predefined properties; toString. */


/* Check the existence of attributes */

= 'rd';
(a in window); // true;

var F = function () {};
= 'RD';
var obj = new F;
('name' in obj); // true;


var toString = ;

// If the object obj contains the method getName, execute it;
if ( && () === '[object Function]') ) {
();
}

// Replenish:
(null == undefined); // true;
(null !== undefined); // true;


/* Delete attributes */
delete ;
// Addition: Using the delete operator, variables declared using var cannot be deleted;


/* Object as an associative array */

// Get object properties:
;
obj['name']; // Here name is a string.

// When using [], the attribute name is represented by a string.
// Add operations during operation
// Note: This property is particularly useful when it is passed as a variable.
// Also known as associative array

/* Mapping: JavaScript objects map strings (attribute names) to values. */
for (var key in obj) {
(key); // key attribute name, which exists as a value here.
}



/*
Common Object properties and methods

All objects in JavaScript are inherited from the Object class;

1, constructor attribute.
Point to its constructor.
*/
var F = function () {};
var f = new F;
( == F); // true

// The prototype existence attribute constructor points to itself;
== F;

// Replenish:
var F = function () {};
var G = function () {};
= new F;

var g = new G;
( == F); // true;
( == G); // false;
// You can use g instanceof F;


/*
2, toString() method
*/
{'name': 'maxthon'}.toString(); // '[object Object]'

/* The array uses the toString method to form a string of elements, and other objects will be converted into [object Object];
The function uses the original toString method and will get the function source code */
['a', 'b', 1, false, ['e','f'], {}].toString();
// "a,b,1,false,e,f,[object Object]"

function t() {
('test');
}
();
// Source code

/*
3, toLocalString();
Returns a localized string of the object
4, valueOf();
When converted to primitive types, . valueOf/toString is used.
5, hasOwnProperty();
6, propertyIsEnumberable();
Whether it can be enumerated;
7, isPrototyeOf();
(b);
If a is a prototype of b, return true;
*/
var o = {}; // new Object;
(o); // true;
(o); // false;
(); // false;
(Object); // true;

/* [The closure is an instance of the function, and the garbage is not recycled, and the assignment reference exists] */



/*
Array: Ordered, valued set;

Each value, also called an element, corresponds to a subscript;
Subscript starts from 0;
The value in the array, can be of any type. Array, object, null, undefined.
*/

// Create.
var arr = [];
var arr = new Array();

var t = '';
var arr = [1,2,3, null, undefined, [], {}, t];

/* 3 cases of creating an array using the new operator: */
var arr = new Array(); // [], the same as direct quantity

var arr = new Array(5); // Length is 5; [] Direct quantity cannot be done.
(arr); // []; JavaScript engine ignores undefined;

var arr = new Array('5'); // The value is ['5'];
var arr = new Array('test'); // The value is ['test'];

/* Related Examples */
var s = [1, 2, 3];
s[5] = 'a';
(s);
[1, 2, 3, undefined, undefined, 'a']



/* Read and write array */

value = array[0];
a[1] = 3.14;
i = 2;
a[i] = 3;
a[a[i]] = a[0];

// Array -> Object -> Properties
= 'rd';

// The array subscript is greater than or equal to 0, and less than 2, and is subtracted by 1 to the power of 32.
// For other values, JavaScript will be converted into strings, as the name of the object attribute, and is no longer a subscript.


var array = [];
array[9] = 10; // The length of array will become 10;
// Note: The JavaScript interpreter only allocates memory to elements with array subscripts of 9, and no other subscripts are included.

var array = [];
= 10; // Add the length of array;
array[] = 4;


/* Delete array elements */
// The delete operator sets an array element to an undefined value, but the element itself still exists.
// To truly delete, you can use: (); [Delete the first one] (); [Delete the last one] (); [Delete a continuous range from an array] or correct the length;

/* Related Examples */
var a = [1, 2, 3];
delete a[1];
(a); // [1, undefined, 3];

/* Supplement: Authoritative JavaScript Guide, Edition 5, Page 59
Variables declared by var are permanent, that is, deleting these variables with the delete operator will throw an error.
But: In the developer tools, it can be deleted. In the web page, as written in the book.
*/


/* Array length */
[].length;


/* traverse array */
var array = [1, 2, 3, 4, 5];
for (var i = 0, l = ; i < l; i++) {
(array[i]);
}

(function (item, index, arr) {
(item);
});

/* Intercept or grow array: Correct length length, mentioned earlier */

/* Multidimensional array */
[[1], [2]]

/* Array method */
// join
var array = [1, 2, 3, 4, 5];
var str = (); // 1,2,3,4,5
var str = ('-'); // 1-2-3-4-5
// Note: This method is the opposite of the () method;

// reverse();
var array = [1, 2, 3, 4, 5];
(); // [5, 4, 3, 2, 1]
// Note: Modify the original array;

// sort();
var array = [1, 3, 2, 4, 5, 3];
();// [1, 2, 3, 3, 4, 5];
/* Note: There are undefined elements in the array, put these elements to the end */

/* You can also customize sorting, sort(func);
func receives two parameters. If the first parameter should be before the second parameter, the comparison function will return a number less than 0, and instead, return a number greater than 0. Equal, return 0;
*/
(function (a, b) {
return b - a;
});

// Example: sorted by odd to even numbers and small to large
[1, 2, 3, 4, 5, 6, 7, 2, 4, 5, 1].sort(function (a, b) {
if (a % 2 && b % 2) {
return a - b;
}

if (a % 2) {
return -1;
}

if (b % 2) {
return 1;
}

return a - b;

});


// concat() method. Merge arrays, but not deep merge
var a = [1, 2, 3];
(4, 5); // [1, 2, 3, 4, 5]
([4, 5]); // [1, 2, 3, 4, 5]
([4, 5], [8, 9]); // [1, 2, 3, 4, 5, 8, 9]
([4, 5], [6, [10, 19]]); // [1, 2, 3, 4, 5, 6, [10, 19] ]


// slice() method. The source array does not change.
var a = [1, 2, 3, 4, 5];
(0, 3); // [1, 2, 3]
(3); // [4, 5];
(1, -1); // [2, 3, 4]
(1, -1 + 5)
(1, 4);
(-3, -2); // [3]
(-3 + 5, -2 + 5);
(2, 3);
/* Note:
Excluding elements specified by the second parameter.
Negative value is converted to: Negative value + array length
*/

// splice(pos[, len[, a, b]]) method. After deleting the specified position, the specified length element is started, and then the element is added;
// Returns an array composed of deleted elements. Change the original array.
var a = [1, 2, 3, 4, 5, 6, 7, 8];
(4); // [5, 6, 7, 8]; At this time a: [1, 2, 3, 4]
(1, 2); // [2, 3]; At this time a: [1, 4];
(1, 1); // [4]; At this time a: [1];

var a = [1, 2, 3, 4, 5];
(2, 0, 'a', 'b'); // [1, 2, 'a', 'b', 3, 4, 5]
(2, 2, [1, 2], 3); // ['a', 'b']; At this time a: [1, 2, [1, 2], 3, 3, 4, 5]
/* Note:
The parameters after the second parameter are directly inserted into the processing array.
The first parameter can be a negative number.
*/


// push() method and pop() method.
// push() can append one or more new elements to the tail of the array and then return the new length of the array;
// pop() deletes the last element in the array, reduces the length of the array, and returns the value it deletes.
// Note: Both methods are modified on the original array, rather than generating a modified array copy.

var stack = [];
(1, 2); // stack: [1, 2]; return 2;
(); // stack: [1]; return 2; deleted element value
(3); // stack: [1, 3]; return 2;
(); // stack: [1]; return 3; deleted element value
([4, 5]); // stack: [1, [4, 5]]returm 2;
(); // stack: [1]; return [4, 5]; deleted element value

// unshift() method and shift() method. Same as above, start from the array header.


// toString() method and toLocalString()
[1, 2, 4].toString(); // 1,2,3;
['a', 'b', 'c'].toString(); // 'a,b,c';
// The same is true for join method without parameters.


/* jsapi new method: map, every, some, filter, forEach, indexOf, lastIndexOf, isArray */


/* array-like object */

arguments
();