JavaScript Advanced Programming Reading notes 2

Source: Internet
Author: User
Tags access properties

Fifth Chapter reference types

 Objects are instances of a reference type, which is a data structure that organizes data and functionality together. Describes the properties and methods that a class of objects have.  An object is an instance of a particular reference type that is created by using the new operator with a constructor, which is itself a function, except that the function is defined for the purpose of creating a new object. eg

var person = new Object ();

1. There are two ways to create an instance of an object: New+object two: Object literal

var person = new Object (); var person = {

person.name = "Andy"; Name: "Andy", //Here is a comma attribute can be enclosed in quotation marks as a string

person.age = 29;   AGE:29}; 29 not after +, because it is the last property

 object literal syntax is recommended only when considering the readability of object property names . , the object constructor is not called when you define objects with this method. General developers prefer the literal syntax because there is less code and a sense of encapsulation. literals are also the preferred way to pass a large number of optional parameters to a function . eg

function DisplayInfo (args) {

var output = "";

if (typeof args.name = = = "string") {

output+= "Name:" +args.name+ "\ n";

}

if (typeof args.age = = "Number") {

output+= "Age:" +args.age+ "\ n";

}

alert (output);

}

DisplayInfo ({

Name: "Andy",

Age:29

}); //Name:andy

Age:29

          

DisplayInfo ({

Name: "Zhou",

}); //Name:zhou

2. In general, point notation is used when accessing object properties, but if the property name contains a character that causes a syntax error, or if the attribute name uses a keyword or a reserved word, use the square bracket notation

eg:person["First Name"], it is generally recommended to use dot notation unless you have to use variables to access properties .

3.Array Type An array is an ordered list of data, but an array in ES can hold any type of data and can be dynamically adjusted when the array is resized .  

4. Basic way to create an array: one using the array constructor var colors =new Array (x); The parameter can represent the initial length of the array as a numeric value

can also be a character that represents the contents of the initial array

Two-array literal notation the var color = [args] parameter can be empty, or the actual array value is separated by commas, but the last character cannot be a comma

As with creating objects, literal notation does not call the array constructor.

5. When reading and setting the value of an array, use square brackets and provide a 0-based numeric index of the corresponding value.  eg:colors[2]//read the third setting: colors[2] = "red"; If the array length is exceeded, it is automatically incremented to the length of the index value +1.

Eg:var num = [0,1,2,3,4] num[0] = 15//set num.length= 5;//add 5 at end num[99] = 99; The 100th item is 99 6-98 for undefined

6. You can use instanceof to detect whether an object is not an array of value instanceof array; But this assumes that in an environment where there is only one global variable, it is necessary to use Array.isarray (X) to detect if multiple frames are included.

7. The array has tolocalestring (), toString (), and valueof () conversion methods. The ToString () method of the call array returns a comma-delimited string that is stitched together as a string of each value in the array. ValueOf () returns an array.

Join () constructs the string using a different delimiter, accepting only one argument, which is used as a delimiter string , and then returning a string containing all the array items

eg:num= [1,2,3,4,5]; Num.join ("!"); /1!2!3!4!5 If the parameter is not passed or the argument is undefined, use a comma as the delimiter  

Push () takes any number of arguments, adds them to the end of the array one by one, and returns the modified array length

Pop () does not remove the last item from the array, reducing the value of length-1, returning the removed item

Shift () does not remove the first item from the array, reducing the value of length-1, returning the removed item           

Unshift() accepts any number of arguments, adds them individually to the array header, and returns the modified array length   

Increases the return array length, reducing the return of the deleted item

Reverse () Reverses the order of array items

  Sort () calls the ToString () transformation method, sorts the array, and the Unicode encoding is small in front. Because it is ToString (), [0,1,5,10,15].sort ()//0,1,10,15,5

The sort () method can accept a comparison function as a parameter. The comparison function accepts two parameters if the first parameter should be preceded by a negative number before the second one is returned

If the first parameter should be located after the second one returns an integer

returns 0 if equal;

Functions are as follows: Function compare (value1,value2) {or function Compare (value1,value2) {

if (value1<value2) return value1-value2

{return-1;} }

Else if (value1>value2) var values = [0,1,5,10,15];

{return 1;} Values.sort (compare);

Else{return 0;}

  } alert (values);//0,1,5,10,15

var values = [0,1,5,10,15];

                    Values.sort (Compare);

alert (values);//0,1,5,10,15

  8.concat (), creates an array copy based on the current array, adds the received parameter (any type) to the end of the copy, and finally constructs a new array .

Slice (), creates a new array based on the current array, accepts 1-2 parameters, which is the starting and ending position to return (excluding the end position). If there is only one argument, the starting item is to the end of the array and does not affect the original array .

   Splice () Delete: Accept 2 parameters The position of the first item to be deleted and the number of items to be deleted splice Delete the second third item

Insert: Accepts 3 parameter start position, 0, the item to insert Splice (2,0, "Red", "green") from the third entry insert Red Green

Replace: Accept the three parameter starting position, the number of items to be deleted, the item to be inserted splice (2,1, "Red", "green") Delete the third item, insert Red Green

9. Find location indexOf (x, "Y") x is the value to find, starting from the beginning, the return value is the X index position, Y is the optional parameter to indicate the starting position

Lastindex starting from the back

        CharAt (num) to find the value of index position num

10. Iterative Methods

The following five methods accept two parameters: the function to run on each item and the [scope object running the function]

    every () runs the given function for each item in the array, and returns True if the function returns true for each item

    some () runs the given function for each item in the array, and returns True if an item returns True

filter ()     Run the given function for each item in the array, picking out the item that returns true to set up a new array

    map () runs the specified function on each item on the original array, returning an array of the results of each function call

ForEach (), which runs the specified function for each item of an array, with no return value

var numbers = [1,2,3,4,5,6,7]

var everyresult = numbers.every (function (Item,index,array) {

return (ITEM>2);

});//false

      

var everyresult = numbers.some (function (Item,index,array) {

return (ITEM>2);

});//true

    

var everyresult = numbers.filter (function (Item,index,array) {

return (ITEM>2);

});//[3,4,5,6,7]

var everyresult = Numbers.map (function (Item,index,array) {

return (ITEM*2);

});//[2,4,6,8,10,12,14]

11.reduce ()// start with the first entry, and iterate through the last

Reduceright ()//Last start

Accepts two parameters: the function on each item and [the initial value as the base of the merge]

var values = [1,2,3,4,5];

var sum = sum.reduce (function (Pre,cyr,index,array) {

return prev+cur;

});//15

JavaScript Advanced Programming Reading notes 2

Related Article

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.