Created
February 18, 2014 16:10
-
-
Save adohe-zz/9073962 to your computer and use it in GitHub Desktop.
some useful piece of code in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1.Get the max and min number in the array of numbers | |
| var arr = [1, 2, 3, 4, 5]; | |
| var max = Math.max.apply(Math, arr); | |
| var min = Math.min.apply(Math, arr); | |
| 2.Empty the array | |
| var arr = [1, 2, 3]; | |
| arr.length = 0; | |
| 3.Don't use delete to move an item in the array | |
| Use splice instead of using delete to delete an item | |
| from an array. Using delete replaces the item with | |
| undefined instead of the removing it from the array. | |
| var arr = [1, 2, 3, 4, 5, 6]; | |
| arr.length; | |
| arr.splice(3, 1); | |
| 4.Use the map() function method to loop through an array’s items | |
| var squars = [1, 2, 3].map(function(val) { | |
| return val * val; | |
| }); | |
| 5.Check the properties of an object when using a for-in loop | |
| var obj = {/***/}; | |
| for(var p in obj) { | |
| if(obj.hasOwnProperty(p)) { | |
| //do something with p | |
| } | |
| } | |
| 6.Create an object whose prototype is a given object | |
| function clone(object) { | |
| function Constructor() { | |
| } | |
| Constructor.prototype = object; | |
| return new Constructor(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment