_javascript techniques of Latent JS object and array

Source: Internet
Author: User
Tags array length object object hasownproperty
Copy Code code as follows:

/*
Arrays and objects "JavaScript Authority Guide, Fifth Edition"
*/

/*
Object: is a collection of unordered attributes, each with its own name and value.

/* Create object Simple method, Object direct quantity * *
var obj = {};
var obj = {name: ' Maxthon '};
var obj = {name: {}, Text: []};

/* You can 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 '
Object is an instance of a Function.
A Function is a special object and an instance of object.
*/

/* Object Properties * *
Use. Conforms to the value of the property being accessed.
Note: You can also use [], which uses the attribute name (which is particularly useful for using variables).
var t = {};
T.text = ' Hello ';
T.O = {};
t.o.name = ' rd ';
T.N = [];

var t = {
"Text": "Hello"
};
Console.log (T.text); ' Hello ';
Add: often use keyword var to declare a variable, but when declaring an object property, you cannot use the Var declaration


/* Object Enumeration * *

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

Enumerate only the objects themselves, not 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 attribute 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 Object obj contains method GetName, execute it;
if (Obj.getname && tostring.call (obj.getname) = = ' [object Function] ') {
Obj.getname ();
}

Add:
Console.log (null = = undefined); True
Console.log (null!== undefined); True


/* Delete attributes/*
Delete Obj.name;
Add: Using the delete operator, you cannot delete a variable that uses the Var declaration;


/* As an associative array of objects/*

Fetch Object Properties:
Obj.name;
obj[' name ']; Here name is a string.

When using [], the property name is represented by a string. Then you can
Adding operations during run
Note: This is especially useful when this property is passed as a variable.
Also known as associative arrays

/* Mapping: A JavaScript object maps a string (property name) to a value. */
for (var key in obj) {
Console.log (key); The key property name, where the value exists.
}



/*
Common Object Properties and methods

All objects in JavaScript are inherited from the object class;

1, constructor property.
Point to its constructor.
*/
var F = function () {};
var f = new F;
Console.log (F.constructor = = f); True

The prototype of the constructor exists attribute constructor pointing to itself;
F.prototype.constructor = = F;

Add:
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
G instanceof F can be used;


/*
2, ToString () method
*/
{' name ': ' Maxthon '}.tostring (); ' [Object Object] '

/* Arrays Use the ToString method to make elements into strings, and other objects are converted to [object];
function using the original ToString method, will get 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

/*
3, tolocalstring ();
Returns a localized string of objects
4, valueof ();
When you convert to a basic type, you use the. Valueof/tostring.
5, hasOwnProperty ();
6, propertyisenumberable ();
Whether it can be enumerated;
7, isprototyeof ();
A.isprototyeof (b);
Returns True if A is a 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 the existence of a function instance, garbage is not recycled is the existence of assignment reference" * *



/*
Array: Ordered, set of values;

Each value, also called element, corresponds to a subscript;
The subscript is starting from 0;
An array of values that 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 things to create an array using the new operator: * *
var arr = new Array (); [], the same as the direct amount

var arr = new Array (5); Length of 5; [] the direct volume is not possible.
Console.log (arr); // []; JavaScript engines ignore undefined;

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

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



/* Read and write the array * *

Value = Array[0];
A[1] = 3.14;
i = 2;
A[i] = 3;
A[a[i]] = a[0];

Array-> Object-> property
array.test = ' rd ';

The array subscript is greater than or equal to 0, and is less than 2 of the 32 times minus 1 integers.
Other values, JavaScript converts to a string, the name of the object attribute, and no longer the subscript.


var array = [];
ARRAY[9] = 10; The array length will change to 10;
Note: The JavaScript interpreter allocates memory only to elements with an array subscript 9, with no other subscript.

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 itself still exists.
Really delete, you can use: Array.shift (); "Delete First" array.pop (); "Delete the Last" Array.splice (); "Delete a contiguous range from an array" or modify the array.length length;

/* Related Instances * *
var a = [1, 2, 3];
Delete A[1];
Console.log (a); [1, Undefined, 3];

* Add: JavaScript Authority Guide Fifth Edition 59 page
Variables declared by Var are permanent, that is, deleting these variables with the delete operator will raise an error.
But: in the developer tool, it can be deleted. and on the Web page, as the book says.
*/


/* Array Length * *
[].length;


/* Traversal 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);
});

/* intercept or Grow array: fixed length, previously mentioned * *

/* Multidimensional 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 contrary 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 last * *

/* can also be custom sorted, sort (func);
Func receives two parameters, and if the first argument should precede the second argument, the comparison function returns a number less than 0, and instead returns a number greater than 0. Equal, return 0;
*/
Array.Sort (function (A, b) {
return b-a;
});

Instance: by odd to even, and sorted from 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. Merging arrays, but not deep merging
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 does not change.
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 element specified by the second parameter.
Negative values into: Negative + array length
*/

Splice (pos[, len[, A, b]]) method. Deletes the specified length element, then adds the element;
Returns an array of deleted elements. The original array is changed.
var a = [1, 2, 3, 4, 5, 6, 7, 8];
A.splice (4); [5, 6, 7, 8]; At this time a: [1, 2, 3, 4]
A.splice (1, 2); [2, 3]; At this time a: [1, 4];
A.splice (1, 1); [4]; At this time 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 ']; At this time a: [1, 2, [1, 2], 3, 3, 4, 5]
* Note:
The arguments after the second argument are inserted directly into the processing array.
The first argument can be negative.
*/


Push () method and Pop () method.
Push () can attach 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 value it deletes.
Note: All two methods are modified on the original array, not 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 () method and the Shift () method. Ditto, starting with the array header.


ToString () method and tolocalstring ()
[1, 2, 4].tostring (); 1,2,3;
[' A ', ' B ', ' C '].tostring (); ' A,b,c ';
Is the same as a join method without parameters.


/* Jsapi New Add Method: 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.