Object Direct Volume

Source: Internet
Author: User
Tags hasownproperty
Definition object of object quantity: it is an unordered property set, and each property has its own name and value. It consists of a list of attribute descriptions. Each attribute description is separated by a comma. Each attribute description consists of an attribute name, a colon, and a property value. Use. the value that meets the access attribute/* array and object [fifth edition of The JavaScript authoritative guide] * // * object: a unordered attribute set, each attribute has its own name and value * // * simple method for creating an object. The object quantity is directly */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' Object is Funct Ion instance. function is a special Object and an instance of the Object. * // * object attributes * // use. the value that matches the access attribute. // 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: variables are usually declared using the keyword var, but when object attributes are declared, you cannot use var Declaration/* object enumeration */var F = function () {}; F. prototype. name = 'RD '; var obj = new F; for (var key in obj) {console. log (key); // na Me;} // only enumerate the object itself. Do not search 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 & String. call (obj. getName) = '[object Function]') {obj. getName () ;}// supplement: console. log (null = undefined); // true; console. log (null! = Undefined); // true;/* delete attribute */delete obj. name; // supplement: When the delete operator is used, variables declared using var cannot be deleted;/* is used as the object of the associated array * // The object attribute is obj. name; obj ['name']; // here name is a string. // when [] is used, the attribute name is represented by a string. so you can // Add and other operations during the running process // Note: This attribute is particularly useful when it is passed as a variable. // also known as correlated array/* ing: A JavaScript Object maps a string (attribute name) to a value. */for (var key in obj) {console. log (key); // key attribute name, which exists as a value here .} /* all objects in Common Object Attributes and methods in JavaScript are inherited from the Object class; 1. construct Or attribute. point 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 '}. toStrin G (); // '[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 (); return a localized string 4, valueOf () of the object; it will be used when converting to a basic type. valueOf/toString. 5, hasOwnProperty (); 6, propertyIsEnumberable (); whether it can be enumerated; 7, I SPrototyeOf ();. isPrototyeOf (B); returns true if a is B's prototype; */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 garbage collection is a value reference.] * // * array: an ordered set 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 ar R = new Array (); var t = ''; var arr = [1, 2, 3, null, undefined, [], {}, t]; /* use the new operator to create an Array in three cases: */var arr = new Array (); // [], which is the 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, undef Ined, '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; // array length will change to 10; // Note: The JavaScript interpreter only allocates memory to elements whose array subscript is 9, no other subscript. var array = []; array. length = 10; // Add the length of the array; array [array. length] = 4;/* delete an array element * // delete an array The element is set 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: the variables declared by var on page 59 of the fifth edition of the JavaScript authoritative guide are permanent, that is, deleting these variables using the delete operator will cause an error. however, it can be deleted in developer tools. in the web page, as written in the book. * // * array length */[]. length;/* traverse array */var array = [1, 2, 3, 4, 5]; fo R (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 the array: corrected the length. We have mentioned * // * multi-dimensional array */[[1], [2]/* array Method * // joinvar array = [1, 2, 3, 4, 5]; var str = array. join (); // 1, 2, 3, 4, 5var str = array. join ('-'); // 1-2-3-4-5 // Note: This method is similar to String. opposite to the 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: The array contains undefined elements, put these elements in the last * // * and you can 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, it returns a number greater than 0. equal, returns 0; */array. sort (function (a, B) {return B-a;}); // instance: sort by odd to even number and in ascending order of [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];. concat (4, 5); // [1, 2, 3, 4, 5]. concat ([4, 5]); // [1, 2, 3, 4, 5]. concat ([4, 5], [8, 9]); // [1, 2, 3, 4, 5, 8, 9]. 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];. slice (0, 3); // [1, 2, 3]. slice (3); // [4, 5];. Slice (1,-1); // [2, 3, 4]. slice (1,-1 + 5). slice (1, 4);. slice (-3,-2); // [3]. slice (-3 + 5,-2 + 5);. slice (2, 3);/* Note: the elements specified by the second parameter are not included. negative value conversion: negative value + array length * // splice (pos [, len [, a, B]) method. after the specified position starts to be deleted, 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];. splice (4); // [5, 6, 7, 8]; a: [1, 2, 3, 4]. splice (1, 2); // [2, 3]; a: [1, 4];. splice (1, 1); // [4]; a: [1]; Var a = [1, 2, 3, 4, 5];. splice (2, 0, 'A', 'B'); // [1, 2, 'A', 'B', 3, 4, 5]. 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 of the array, reduce the length of the array and return 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; the 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 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 * // * an array-like object */argumentsdocument. 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.