In Operator:
<script>/*1. Ability to traverse the properties of an object*/ varobj = { ' Name ': ' Jack ', Age:20 }; for(varKeyinchObj) {console.log (key+ ":" +obj[key]); } /*2. You can also determine whether an object can access a property, regardless of whether the property is defined in the current object*/Console.log (' Name 'inchobj);//trueConsole.log (' toString ')inchobj);//trueConsole.log (' toString1 'inchobj);//false</script>
Use of Foreach:
<script>/*foreach is used to iterate over an array*/ //var arr= [123,2,4,35,435,65,7,658,67,98]; /*Value: The current traversal to the element value Index: current element indexed arr: the currently traversed array **/ /*Arr.foreach (function (value,index,arr) {console.log (value + ":" + index + ":" + arr); })*/ /*Customizing the foreach function*/Array.prototype.myForEach=function(callback) { for(varI =0;i<arr.length;i++) {callback ( this[i],i, this); } } vararr1= [123,2,4,35,435,65,7,658,67,98]; Arr1.myforeach (function(value,index) {console.log (value+ ":" + index + ":" +arr1); });</script>
Use of Map:
<script>varArr= [123,2,4,35,435,65,7,658,67,98]; /*double the array value and store it in an array*/ /*1. Use the for*/ //var newarr=[]; /*for (var i = 0; i < arr.length; i++) {var obj = arr[i]; Newarr.push (obj*2); } console.log (newArr);*/ /*2. Using the foreach*/ /*Arr.foreach (function (value,index,arr) {newarr.push (value*2); }); Console.log (newArr);*/ /*3. Using the map function*/ /*arr.map (function (value,index,arr) {newarr.push (value*2); }); Console.log (newArr);*/ /*Map stores the results in a newly generated array and returns*/ /*var newArr = arr.map (function (value,index,arr) {return value * 2; }); Console.log (newArr);*/ /*how map is implemented*/Array.prototype.myMap=function(callback) {vartemp = []; for(vari=0;i< this. length;i++){ varv = Callback ( this[i],i, this); if(v) {temp.push (v); } } returntemp; }; varNEWARR = Arr.mymap (function(value,index,arr) {returnValue * 2; }); Console.log (newArr);</script>
JS in operator, use of foreach and map-basic knowledge Summary------peng kee (016)