JS more useful to collect some methods

Source: Internet
Author: User

JS more useful to collect some methods

Math.ceil (x) -Returns the smallest integer greater than or equal to the number parameter (rounding function) to round the number

Math.floor (x)-Returns the largest integer less than or equal to the number parameter, rounding the number down

Join ()


Example: var a = ["A", "B", "C"]; To output "A,b,c" requires a. Join (', ') to concatenate array items with ","

If you want to output "ABC" direct A. Join (‘‘)

Push ()

The

    method adds these elements in the order in which the new elements appear. If one of the parameters is an array, it is added as a single element to the array. &NBSP

   example: Var   a=[1,2,3,4]   A. push (5);  array A will have 1,2,3,4,5 five elements

p>   Note: the  push () parameter can be multiple values ( param1,param,... Paramn ), but cannot be an object

              var arr1 = [1, 3, 4];
             var arr2 = [3, 4, 5];

               if using Arr1.push (ARR2) The array arr1 get [1,3,4,[3,4,5]] instead of [1,3,4,3,4,5]

              If you want to get [1,3,4,3,4,5] with   Array.prototype.push.apply (arr1, ARR2); or Arr1=arr1.concat (ARR2)  concat method does not change the arr1 itself

               but you can Arr1.push (5,6) to get [1,3,4,5,6]

AAA

ToFixed ()

Rounded

Example: Var x=0.02345 x.toFixed (2) results for 0.002 x=0.0564 result is 0.06

Apply ()

Apply: Methods can hijack another object's method, inheriting another object's properties.

Function.apply (Obj,args) method can receive two parameters
OBJ: This object will replace the This object in the function class
Args: This is an array, which is passed as a parameter to function (args-->arguments)

Example:/* Define a human */
function person (name, age) {
This.name= name;
This.ag= age;
}
/* Define a student class */
function Student (name, age, Grade)
{
Person.apply (this, arguments); This is student this object arguments as an incoming parameter collection
This.grade = grade;
}

When we create the object var object=new Student (' aking ', ' one '); At this time execution student when executed to person.apply (this, arguments);

At this point, the this object in person will be replaced by the student object and executed by the person method student the object in the person method body. Name= "aking";

Student object. age=26;

Apply () Magical: Math.max (Param1,param2,param3 ...) ==>var max=math.max.apply (Null,array)

Math.min (Param1,param2,param3 ...) ==>var min=math.min.apply (Null,array)

Push (Param1,param,... paramn) ==>array.prototype.push.apply (ARR1,ARR2);
In general, the target function requires only n argument lists, not the form of an array ([param1[,param2[,... [, Paramn]]] ), you can solve this problem skillfully by apply way!

map () (jquery method)

 jquery.map (array,callback)   converts elements in one array into another array. The conversion function as a parameter is called for each array element, and the conversion function is passed a parameter that represents the transformed element. The conversion function can return a converted value, null (delete an item in an array), or an array containing a value, and extend to the original array.

Parameter
Array: the array to be converted. Callback (Function): called for each array element, and the conversion function is passed a representation of the transformed element as an argument. function to return any value. In addition, this function can be set to a string that, when set to a string, is considered "Lambda-form" (abbreviated form?). ), where a represents an array element. such as "A * a" means "function (a) {return a * A;}". The

Example
converts each element in the original array to a new array, plus 4.

JQuery code:

$.map ([0,1,2], function (n) {
return n + 4;
});
Result:

[4, 5, 6]

--------------------------------------------------------------------------------

The element in the original array is greater than 0 plus 1, otherwise it is deleted.

JQuery code:

$.map ([0,1,2], function (n) {
return n > 0? n + 1:null;
});
Result:

[2, 3]

 eval: The function receives a parameter s, if s is not a string, return directly s. Otherwise execute s statement. (eval cannot execute in global space)

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.