Tips for js objects and array _ javascript

Source: Internet
Author: User
Tags hasownproperty
The js object and array object are unordered attribute sets, and each attribute has its own name and value. The Code is as follows:


/*
Array and object [fifth edition of JavaScript authoritative guide]
*/

/*
Object: an unordered property set. Each property has its own name and value */

/* Simple object creation method, direct object quantity */
Var obj = {};
Var obj = {name: 'maxthon '};
Var obj = {name :{}, text: []};

/* Use the new operator */
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'
The Object is a Function instance.
Function is a special Object and an instance of the Object.
*/

/* Object attributes */
// Use. Match to access the attribute value.
// Note: [] can be used at the same time, with the attribute name (variables can be used, which is particularly useful ).
Var t = {};
T. text = 'hello ';
T. o = {};
T. o. name = 'RD ';
T. n = [];

Var t = {
"Text": "hello"
};
Console. log (t. text); // 'hello ';
// Supplement: the keyword var is usually used to declare variables. However, when declaring object attributes, var declaration cannot be used.


/* Object enumeration */

Var F = function (){};
F. prototype. name = 'RD ';
Var obj = new F;
For (var key in obj ){
Console. log (key); // name;
}

// Only enumerate the object itself and do not look up along the prototype chain
For (var key in obj ){
If (obj. hasOwnProperty (key )){
Console. log (key );//
}
}
/* Note: for in cannot enumerate predefined attributes; toString .*/


/* Check the property existence */

Window. a = 'RD ';
Console. log (a in window); // true;

Var F = function (){};
F. prototype. name = 'RD ';
Var obj = new F;
Console. log ('name' in obj); // true;


Var toString = Object. prototype. toString;

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

// Supplement:
Console. log (null = undefined); // true;
Console. log (null! = Undefined); // true;


/* Delete attributes */
Delete obj. name;
// Supplement: using the delete operator, variables declared using var cannot be deleted;


/* As the object of the associated array */

// Get object attributes:
Obj. name;
Obj ['name']; // here name is a string.

// When [] is used, the attribute name is represented by a string.
// Add and perform other operations during the running process
// Note: This attribute is particularly useful when passed as a variable.
// Also known as correlated Array

/* Ing: the JavaScript Object maps a string (attribute name) to a value .*/
For (var key in obj ){
Console. log (key); // name of the key attribute, which exists as a value.
}



/*
Common Object Attributes and Methods

All objects in JavaScript are inherited from the Object class;

1. constructor attributes.
Point to its constructor.
*/
Var F = function (){};
Var f = new F;
Console. log (f. constructor = F); // true

// The constructor attribute of the constructor refers to itself;
F. prototype. constructor = F;

// Supplement:
Var F = function (){};
Var G = function (){};
G. prototype = new F;

Var g = new G;
Console. log (g. constructor = F); // true;
Console. log (g. constructor = G); // false;
// You can use g instanceof F;


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

/* The array uses the toString method to construct a string consisting of elements. Other objects are converted to [object Object].
The function uses the original toString method to obtain the function source code */
['A', 'B', 1, false, ['E', 'F'], {}]. toString ();
// "A, B, 1, false, e, f, [object Object]"

Function t (){
Console. log ('test ');
}
T. toString ();
// Source code

/*
3, toLocalString ();
Returns a localized string of an object.
4, valueOf ();
The. valueOf/toString.
5, hasOwnProperty ();
6, propertyIsEnumberable ();
Can be enumerated;
7. isPrototyeOf ();
A. isPrototyeOf (B );
True is returned if a is the prototype of B;
*/
Var o = {}; // new Object;
Object. prototype. isPrototyeOf (o); // true;
Object. isPrototyeOf (o); // false;
O. isPrototyeOf (Object. prototype); // false;
Function. prototype. isPrototyeOf (Object); // true;

/* [Closure is a function instance, and there is a value reference for garbage collection ]*/



/*
Array: an ordered set of values;

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

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

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

/* Use the new operator to create an array :*/
Var arr = new Array (); // [], same as the direct quantity

Var arr = new Array (5); // The length is 5; [] the direct Quantity cannot be achieved.
Console. log (arr); // []; the JavaScript engine ignores undefined;

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

/* Related instance */
Var s = [1, 2, 3];
S [5] = 'a ';
Console. log (s );
[1, 2, 3, undefined, undefined, 'a']



/* Read and write arrays */

Value = array [0];
A [1] = 3.14;
I = 2;
A [I] = 3;
A [a [I] = a [0];

// Array-> Object-> attribute
Array. test = 'RD ';

// Array subscript greater than or equal to 0, and smaller than the 32 power of 2 minus an integer of 1.
// For other values, JavaScript will be converted into a string and used as the object attribute name. It is no longer a subscript.


Var array = [];
Array [9] = 10; // The array length is changed to 10;
// Note: The JavaScript interpreter only allocates memory to the elements whose subscript is 9, and no other subscript exists.

Var array = [];
Array. length = 10; // Add the length of the array;
Array [array. length] = 4;


/* Delete array elements */
// The delete operator sets an array element to the undefined value, but the element still exists.
// Delete the object. You can use Array. shift (); [Delete the first entry] Array. pop (); [Delete the last one] Array. splice (); [delete a continuous range from an Array] or modify Array. length;

/* Related instance */
Var a = [1, 2, 3];
Delete a [1];
Console. log (a); // [1, undefined, 3];

/* Supplement: page 59 of the fifth edition of the JavaScript authoritative guide
Variables declared by var are permanent. That is to say, deleting these variables using the delete operator will cause an error.
However, in developer tools, they can be deleted. In web pages, as written in a book.
*/


/* Array length */
[]. Length;


/* Traverse the array */
Var array = [1, 2, 3, 4, 5];
For (var I = 0, l = array. length; I <l; I ++ ){
Console. log (array [I]);
}

Array. forEach (function (item, index, arr ){
Console. log (item );
});

/* Truncate or increase an array: Modify the length, as mentioned earlier */

/* Multi-dimensional array */
[[1], [2]

/* Array Method */
// Join
Var array = [1, 2, 3, 4, 5];
Var str = array. join (); // 1, 2, 3, 4, 5
Var str = array. join ('-'); // 1-2-3-4-5
// Note: This method is opposite to the String. split () method;

// Reverse ();
Var array = [1, 2, 3, 4, 5];
Array. reverse (); // [5, 4, 3, 2, 1]
// Note: modify the original array;

// Sort ();
Var array = [1, 3, 2, 4, 5, 3];
Array. sort (); // [1, 2, 3, 3, 4, 5];
/* Note: There are undefined elements in the array. Put these elements to the end */

/* You can also customize the sorting, sort (func );
Func receives two parameters. If the first parameter is located before the second parameter, the comparison function returns a number smaller than 0. On the contrary, the number greater than 0 is returned. equal, and 0 is returned;
*/
Array. sort (function (a, B ){
Return B-;
});

// Instance: sorted by odd to even numbers in ascending order
[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 the array, but not deeply merge
Var a = [1, 2, 3];
A. concat (4, 5); // [1, 2, 3, 4, 5]
A. concat ([4, 5]); // [1, 2, 3, 4, 5]
A. concat ([4, 5], [8, 9]); // [1, 2, 3, 4, 5, 8, 9]
A. concat ([4, 5], [6, [10, 19]); // [1, 2, 3, 4, 5, 6, [10, 19]


// Slice () method. The source array is not changed.
Var a = [1, 2, 3, 4, 5];
A. slice (0, 3); // [1, 2, 3]
A. slice (3); // [4, 5];
A. slice (1,-1); // [2, 3, 4]
A. slice (1,-1 + 5)
A. slice (1, 4 );
A. slice (-3,-2); // [3]
A. slice (-3 + 5,-2 + 5 );
A. slice (2, 3 );
/* Note:
Does not include the elements specified by the second parameter.
Negative value conversion: negative value + array Length
*/

// Splice (pos [, len [, a, B]) method. After deleting a specified position, specify the length element, and then add the element;
// Return the array composed of the deleted elements. The original array is changed.
Var a = [1, 2, 3, 4, 5, 6, 7, 8];
A. splice (4); // [5, 6, 7, 8]; a: [1, 2, 3, 4]
A. splice (1, 2); // [2, 3]; a: [1, 4];
A. splice (1, 1); // [4]; a: [1];

Var a = [1, 2, 3, 4, 5];
A. splice (2, 0, 'A', 'B'); // [1, 2, 'A', 'B', 3, 4, 5]
A. splice (2, 2, [1, 2], 3); // ['A', 'B']; 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 () and pop () methods.
// Push () can append one or more new elements to the end 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 deleted Value.
// Note: both methods are modified on the original array instead of generating a modified array copy.

Var stack = [];
Stack. push (1, 2); // stack: [1, 2]; return 2;
Stack. pop (); // stack: [1]; return 2; deleted element value
Stack. push (3); // stack: [1, 3]; return 2;
Stack. pop (); // stack: [1]; return 3; deleted element value
Stack. push ([4, 5]); // stack: [1, [4, 5] returm 2;
Stack. pop (); // stack: [1]; return [4, 5]; deleted element value

// Unshift () and shift () methods. Same as above, starting from the array header.


// ToString () method and toLocalString ()
[1, 2, 4]. toString (); // 1, 2, 3;
['A', 'B', 'C']. toString (); // 'a, B, C ';
// The join method without parameters is the same.


/* New Methods for adding jsapi: map, every, some, filter, forEach, indexOf, lastIndexOf, isArray */


/* Objects similar to arrays */

Arguments
Document. getElementsByTagName ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.