JavaScript Foundation Fifth Day

Source: Internet
Author: User
Tags class definition floor function pow


First, Introduction


We discussed some of the basic concepts of the function, because the function is important in any language, so we should learn it well. When I opened my blog yesterday, I saw someone's private messages. My JavaScript was wrong, and I looked intently at the wrong words. I am sorry to say that, I hope you can see the time to spray me more than two words to forget this thing.





Second, import





Today we are going to talk about the content of the object, the first thing we want to touch is some built-in objects.


third, the key content


① object:



the list reads: "An unordered collection of attributes, each of which holds an original value, object, or function." Because There is no real class in JS, the class definition is described as the formula for the object. We also call the object definition class, because most developers are more familiar with the term, and functionally, the two are equivalent ""



② built-in objects:



Built-in objects are objects that the system has defined for us (similar to Math,Date,Array , and so on).



③math Object methods:



1) Math.ceil (): ceiling function, rounding up.





Console.log (Math.ceil (9.9));


2) Math.floor (); floor function, rounding down.


Console.log (Math.floor (9.9));


3) Math.Abs (); gets the absolute value of the number.


Console.log (Math.Abs (-9.9));


4) math.random (); Get 0-1 the random number.


Console.log (Math.random ());


5) Math.pow (); Get x of the y The power of the second.


Console.log (Math.pow (10,2));


6) Math.max (); gets the maximum value in two digits.


Console.log (Math.max (10,20));


7) math,min (); gets the minimum value in two digits.





Onsole.log (Math.min (10,20));





④date Object methods:



1) Conversion Date:


var New Date (2016,06,10); Console.log (date);


2) returns the number of milliseconds 1970 years 1 months 1 days midnight to a specified date (string).


var date = Date.parse ("2016-08-02") console.log (date);





3) milliseconds to get the date directly


        1. in the HTML5 format
var date = date.now (); Console.log (date);





2. The recommended format


var date = +new  date (); Console.log (date) ;





4) Get the specified part of the date


          1. Returns a 4 -digit year
var year = date.getfullyear (); Console.log (year);








2. Return month starting from 0


var month = date.getmonth (); Console.log (month);








3. Return the day ordinal of the month


var day = date.getdate (); Console.log (day);





4. number of hours to return 0-23


var hours = date.gethours (); Console.log (hours);








5. Returns the minute number of 0-59


var minutes = date.getminutes (); Console.log (minutes);








6. number of seconds to return 0-59


var seconds =








7. Returns the same number of milliseconds as the valueOf () result


var Milliseconds = date.gettime (); Console.log (Milliseconds);





8. Returns the number of milliseconds


var Milliseconds = date.getmilliseconds (); Console.log (Milliseconds);








9. Return day of the week


var day = date.getday (); Console.log (day);











⑤array Object methods:



1) Slice method:Slice (start,end) [Start end](head does not take the tail)


        1. When the value of end is positive:


Take from start , not end       


Console.log (Arr.slice (0,2));








2. When the value of end is negative:



If end is a negative number, the index is the length + negative number of the array


Console.log (Arr.slice (0,-2));








2) Convert an array:


        1. ToString converts an array into a character turn.
Console.log (Arr.tostring ());  


2. ValueOf returns the array object itself



3) Gets the index of an element in the array


        1. IndexOf returns the subscript of an array (where there are two elements)


A) one parameter:


Console.log (Arr1.indexof (1));








b) Two parameters: the second parameter specifies where to start the search


Console.log (Arr1.indexof);


4) Append and delete


        1. Push (); added to the last face of the array.
Arr.push ("abc"); Console.log (arr); 





2. Pop (); deletes the last of the array.


Arr.pop (); Console.log (arr);





3. Shift (); deletes the first of the array.


Arr.shift (); Console.log (arr);





4. Unshift (); added to the front of the array.


Arr.unshift (1); Console.log (arr);








5) Array Sorting


        1. Reverse (); flips the elements in the array.
Arr.reverse (); Console.log (arr);





2. Sort (); to sort the elements in an array


Arr.sort (function(b) { return A-b;}); Console.log (arr); 








6) Array Iteration


        1. filter (); true Left, the return value is false the house.
var arr = arr.filter (function(element,inedx,num) { if(element<5) {  return true  }else{ returnfalse;}    }); console. log (arr);


2. ForEach (); facilitates every element in the loop array


var arr3= Arr.foreach (function(element,index,num) {    console.log (element);    Console.log (index);})








7) Array Empty


        1. Array.splice (0,arr.length); Delete all items in the array
        2. array.length=0; Length can be read and copied, assigned to 0 empty array
        3. Array = []


8) manipulating arrays


        1. Join () to string elements in an array by making a symbol
var arr4 = Arr.join ("|" ) Console.log (ARR4)





2. Concat (); Connect two arrays


var arr5 = arr.concat (arr2); Console.log (ARR5);





3. Slice intercepts a new array from the current array, without affecting the original array. Parameter start starting from 0 , end starting from 1 , end not intercepted.


var arr6 = Arr.slice (0,3) Console.log (ARR6);





4. Delete or replace some items of the current array, parameter start,deletecount,options ( the item to replace ),


var arr7 = Arr.splice (0,3); Console.log (ARR7);


⑥ String part:



1) Gets the index in the string:


        1. Inidexof Returns the position of the specified content in the string
Console.log (Str.indexof ("W"));





2. LastIndexOf from behind, looking only for the first match .


Console.log (Str.lastindexof ("L"));








2) Remove blank:trim () only removes whitespace before and after the string


var str1 = "Hello World"console.log (STR1); Console.log (Str1.trim ());








3) string Conversion case:


        1. Turn Capital touppercase ()
Console.log (Str.touppercase ()); 





2. Turn lowercase tolowercase ()


Console.log (Str.tolowercase ());








4) String manipulation methods:


        1. Concat (); stitching strings, equivalent to + , + more commonly
Console.log (Str.concat (str1));





2. Slice (); from Start position to start, intercept to End location, End not be taken


Console.log (Str.slice (0,3));


3. substring (); from Start position to start, intercept to End location, End not be taken


Console.log (str.substring (0,3));  


4. substr (); from Start position start, intercept length characters, and only one parameter is truncated to the last


Console.log (Str.substr (0,4)); 





5. replace (); replaces an element of a string , and returns the replaced string


Console.log (Str.replace ("H", "H"));





6. Split (); splitting a string ( conversion ) Array


Console.log (Str.split (0,4));


7. The need to differentiate Yes is substring (); substr (); Slice ( )



A) when there is only one positive parameter:



is returned from the string following the start of the positive argument.


Console.log (Str.slice (1)), Console.log (Str.substr (1)), Console.log (str.substring (1));








b) When there is only one negative parameter:


console.log (str.slice (-1)); / Pass in a negative number, which is equivalent to passing in length + (-1), take it backwards, which is equivalent to taking two backwards
console.log (str.substr (-1)); Passing in a negative number is equivalent to passing in length + (-1) // intercepted backwards
console.log (str.substring (-1)); Pass in a negative number, which is equivalent to passing in 0, which is equivalent to all interception








c) When there are two positive parameters


console.log (str.slice (0,1)); take the specified index number
console.log (str.substr (0,1)); intercepts the specified number of characters
console.log (str.substring (0,1)); take the number of the index








D) When there is a positive and negative


console.log (str.slice (0, -1)); The second parameter is also equivalent to length + (-1)
console.log (str.substr (0, -1)); The second parameter, if the negative number is equal to 0, it cannot be intercepted, and it is empty
console.log (str.substring (1, -5)); The second parameter, if it is negative, is equivalent to 0, but this method will start from the smallest of the two parameters as the starting value 
Iv. Summary


Feel a little tired after summing up, I hope you can have some additions.









JavaScript Foundation Fifth Day


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.