Home > Web Front-end > JS Tutorial > Hidden talk about js objects and arrays_javascript skills

Hidden talk about js objects and arrays_javascript skills

WBOY
Release: 2016-05-16 18:06:13
Original
979 people have browsed it
Copy code The code is as follows:

/*
Arrays and Objects [The Definitive Guide to JavaScript, Fifth Edition]
*/

/*
Object: is an unordered set of attributes, each attribute Each has its own name and value*/

/* Simple method to create an object, object direct quantity*/
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: new The operator is followed by the constructor, so
typeof Array; // 'function'
typeof Object; // 'function'
Object is an instance of Function.
Function is a special object, also of Object Example.
*/

/* Object attributes*/
// Use. Conform to access the value of the attribute.
// Note: [] can be used at the same time, and attributes are used inside Name (you can use variables, this 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, but when declaring object properties, var cannot be used to declare


/* 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, 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 properties; 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 the object obj contains the method getName, 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 attribute*/
delete obj.name;
// Supplement : Using the delete operator, variables declared using var cannot be deleted;


/* Object as an associative array*/

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

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

/* Mapping: JavaScript object handle String (attribute name) is mapped to a value. */
for (var key in obj) {
console.log(key); // key attribute name, exists here as a value.
}



/*
Common Object properties and methods

All objects in JavaScript inherit from the Object class;

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

// The prototype of the constructor has the attribute constructor pointing 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]'

/* Arrays use toString method, Combine the elements into a string, and other objects will be converted into [object Object];
The function uses the original toString method, and the function source code will be obtained*/
['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 the object
4, valueOf();
will be used when converting to a basic type. valueOf/toString.
5, hasOwnProperty();
6, propertyIsEnumberable();
Whether it can be enumerated;
7, isPrototyeOf();
a.isPrototyeOf(b);
If a is the prototype of b, return true;
* /
var o = {}; // new Object;
Object.prototype.isPrototyeOf(o); // true;
Object.isPrototyeOf(o); // false;
o. isPrototyeOf(Object.prototype); // false;
Function.prototype.isPrototyeOf(Object); // true;

/* [Closures exist when function instances exist. If garbage is not recycled, assignment references exist. ] */



/*
Array: an ordered collection of values;

Each value, also called an element, corresponds to a subscript;
The 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];

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

var arr = new Array(5); // Length is 5; [] direct quantity cannot be achieved.
console.log(arr); // []; JavaScript engine will ignore 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';
console.log(s);
[ 1, 2, 3, undefined, undefined, 'a']



/* Reading and writing of arrays*/

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

// array -> Object-> Attribute
array.test = 'rd';

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


var array = [];
array[9] = 10; / / The length of the array will become 10;
// Note: The JavaScript interpreter only allocates memory to the element with array index 9, and no other indexes.

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


/* Delete array elements*/
// delete operator An array element is set to an undefined value, but the element itself still exists.
// To actually delete, you can use: Array.shift(); [Delete the first one] Array.pop(); [Delete the last one] Array .splice(); [Delete a continuous range from an array] or modify the Array.length length;

/* Related examples*/
var a = [1, 2, 3];
delete a[1];
console.log(a); // [1, undefined, 3];

/* Supplement: The Definitive Guide to JavaScript, fifth edition, page 59
by var The declared variables are permanent, that is to say, using the delete operator to delete these variables will cause an error.
But: In the developer tools, they can be deleted. And in the web page, as mentioned in the book Write.
*/


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

/* intercept or grow Array: Correct length, as mentioned before*/

/* 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 opposite to the String.split() method;

// reverse() ;
var array = [1, 2, 3, 4, 5];
array.reverse(); // [5, 4, 3, 2, 1]
// Note: Modified 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 at the end*/

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

// Example: Sort from odd to even, and 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. Combine arrays, but not depth 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 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 ​​are converted to: negative Value array length
*/

// splice(pos[, len[, a, b]]) method. After deleting the specified position, specify the length element, and then append the element;
// Return an array composed 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 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 convert one or more appends a new element to the end of the array, and then returns 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 deleted.
// Note: Two Each method modifies the original array instead of generating a modified copy of the array.

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 shift() Method. Same as above, starting from the head of the array.


// toString() method and toLocalString()
[1, 2, 4].toString(); // 1,2,3 ;
['a', 'b', 'c'].toString(); // 'a,b,c';
// Same as the join method without parameters.


/* New jsapi methods: map, every, some, filter, forEach, indexOf, lastIndexOf, isArray */


/* Array-like object*/

arguments
document.getElementsByTagName();
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template