Introduction to the object:
Description: A class/type is a data structure used to organize data and functionality together, although JS is an object-oriented language that does not have a traditional object-oriented approach to customizing the class structure
Object-Related:
Description: An object instance does not have much functionality but is an ideal choice for storing and transmitting data in an application
Create object// - first Way var newobj = new object ()// - the second way var newobj = new object// - The third Way var newobj = { name: "Li full", // Object properties Support quotes around ' age ': 25, "sex": ' man '}// add attribute// - first Way newobj.name = "Liu Jianjin"// - second way newobj[' age '] = 24// read properties// - First Way Console.log (Newobj.name)// - Second Way Console.log (newobj[' age '))// Traverse property for (Key in newobj) { console.log (KEY,  ' ', newobj[key])}// object Method // - The first way var newobj = { ' ToRun ': function () { return ' Running ' }}console.log (Newobj.torun ())// Delete Properties Delete newobj.torun
function Usershow (userInfo) {///can pass UserInfo object in to its properties operation for (key in UserInfo) {Console.log (key, '-a ', Userin Fo[key])}}userinfo = {' UserName ': "Li full", ' Age ': 25}usershow (UserInfo)
Note: An object can be passed as an argument to a function, and an object can carry countless attributes, so the preferred way to pass an optional parameter to a function is commonly used in real-world development
Array-Related:
Description: The array in JS each element can hold any type, and the size can be adjusted, but the array contains up to 4,294,967,295 elements, the exception will occur
Create an array (you can omit new)// - the first way Var newarr = new array ()// - the second way Var newarr = new array (Ten)// - Third Way Var newarr = new array (' Li full ', 25, ' man ')// - Fourth Way var newarr = [' Li full ', 25, ' man ']// Get array length Var arrlen = newarr.lengthconsole.log (arrlen)// array element assignment newarr[0] = ' 刘珍珍 ' newarr[newarr.length] = ' Xxoo ' Console.log (NEWARR)// slightly complex array var newarr = [ { name: ' Li full ', age: 25, sex: ' Men ' }, [' li full ', 25, ' man '], ', ' man '
Object method:
Description: Objects and arrays all have tolocalstring ()/tostring ()/valueof (), where toString () and ValueOf () will return the same value regardless of who is overridden
var userarr = [' Li full ', 25, ' Male ']//implicitly calls ToString () alert (Userarr) Console.log (//Does not call ToString () Userarr,//And valueof () returns a consistent userarr.tostring (),//And ToString () returns a consistent userarr.tolocalestring ())//using the Join () method, you can use different delimiters to build the string Console.log (Userarr.join (' | ‘))
Array Stack method:
Description: An array can operate like a stack, which is a data structure (LIFO), which means that the most recently added element is first removed, and that the elements in the stack (push ()) or eject (pop ()) occur only in one position, the top of the stack, and push () supports receiving any number of arguments. Add them to the end of the array one by one and return the modified array length, while the Pop () method removes the last element from the end of the array, reduces the value of the length of the array, and then returns the removed element
var userInfo = [' Li full ', 25, ' male ']var Newlen = Userinfo.push (' hangzhou Fuyang ') console.log (' The array is pressed into the stack after the length changed to: ' + Newlen) var LastItem = us Erinfo.pop () console.log (' array popup latest press-in data: ' + LastItem)
Array Queue method:
Description: The queue method is FIFO, the queue adds elements at the end of the array, removes elements from the front end of the array, adds an element to the end of the array via push (), and then removes an element from the front end of the array through the shift () method, with a UNSHITF () method, and SHITF () Instead, an element is added to the front end of the array, but the Unshift () method in IE browser returns not the length of the array but undefined
var userInfo = [' Li full ', 25, ' male ']var Newlen = Userinfo.push (' hangzhou Fuyang ') console.log (' The array is pressed into the queue after the length changed to: ' + Newlen) var Shitfitem = Userinfo.shift () console.log (' array ' of elements removed from the head of the queue is: ' + Shitfitem ' var Newlen = userinfo.unshift (' li-Full ') Console.log (' Length after inserting an element into the head of the array: ' + Newlen)
Array sorting methods:
Description: Sort by default in the form of a string comparison, resulting in unexpected comparisons, often need to write their own comparer function, passed to the sort, if the order from large to small, the comparator is greater than return 1 less than return-1 otherwise return 0
var Numarr = [1, 2, 3, 4, 5]console.log (//In reverse order, the original array is modified, returns the sorted array numarr.reverse (),//positive order, the original array is modified, returns the sorted array num Arr.sort ()) var Numarr = [' 1f ', ' 11f ', ' 2f ', ' 22f ']//define comparer function cmp (x, y) {var xval = parseint (x) var yval = P Arseint (y) return xval>yval?1: (xval==yval?0:-1)}//incoming Comparator alert (Numarr.sort (CMP))
Array manipulation methods:
Description: JS has provided many methods for the elements contained in the array, concat (Arrayx ...) Merge array, slice (start, end) gets an array of specified area elements, splice () operations on the source array insert/delete/replace
var userinfo = ["Li full", 25, ' man ']// array merge-.concat (Arrayx ...) You can create a new array based on the current array, the source array is unaffected// - the first way, the parameter supports the array, The return value is the combined array var newinfo = userinfo.concat ([' Wuhan ']) Console.log (newinfo)// - second Way, parameter supports non-array, return value is merged array var newinfo = userinfo.concat (' Wuhan ' , ' 18814818764 ') Console.log (newinfo) Note that the: parameter of the Concat array object can be an array or non-arrays, and also support null, which is equivalent to deep copy// array cut in the array -.slice (Start, end) can get the specified range element based on the current array and create a new array that contains the start index but does not contain the end index element, and the end index supports complex numbers var newinfo = Userinfo.slice (0, 3) console.log (newinfo)// The usage of -is to write the rating component Var rate = 3document.write ("★★★★★☆☆☆☆☆". Slice (5 - rate, 10 - rate)) Array Delete -.splice (INDEX, HOWMANY, ITEM1...ITEMX), if only the first two parameters are truncated, then the 2 element returned from 0, the source array is deleted Var Deleteditems = userinfo.splice (0, 2) console.log (deleteditems) console.log (userInfo)// array insertion -.splice (INDEX, HOWMANY, ITEM1...ITEMX), if multiple arguments are passed, the item1 will be used. Itemx these elements to cover howmany the original elements, if the Howmany is 0, in the position of the index directly inserted item1....itemxuserinfo.splice (0, 0, ' li full ', Userinfo.splice (0, 1, ' Liu Jianjin ') Console.log (UserInfo)
This article is from the "ζ Automated operation and maintenance development Road ζ" blog, please be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1846128
Site Frontend _javascript.0008.javascript Object array