Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. В JavaScript има 4 вида цикли:
  2. 1.while loop:
  3. While(condition) {
  4. Statements;
  5. }
  6. 5,”non-empty” are evaluated as true
  7. 0,”” are evaluated as false
  8.  
  9. 2.do while loop:
  10. Do{
  11. Statements;
  12. }while(condition);
  13.  
  14. 3.for loop:
  15. for (var i = 0; i < 10; i++) {
  16. }
  17. !!! i += 1 е малко по-бързо от i++
  18. !!!можем да имаме повече от един брояч
  19.  
  20. 4.for-in loop-итерира по свойствата (ключовете )на обектите, и по индексите на масивите:
  21. var arr = [10, 20, 30, 40, 50];
  22. for (var index in arr) {
  23. console.log(arr[index])
  24. }
  25. Iterating over the properties of an object:
  26. var obj = { name: 'Steve', age: 23, location: 'Sofia' };
  27. for (var key in obj) {
  28. console.log(obj[key]);
  29. }
  30. // Steve, 23 , Sofia
  31.  
  32. Arrays
  33. Създаване на масив:
  34. var numbers=[1,2,3,4,5];
  35. Елементите на масивите не е задължително да са от един и същи тип!
  36.  
  37. Using new Array(initialLength):
  38. var arr = new Array(10); // [undefined × 10]
  39. методи при масивите:
  40. array.unshift(element)- inserts a new element at the beginning of the array
  41. array.push(element)-appends a new element at the end
  42. array.shift()-removes and returns the element at the beginning of the array
  43. array.pop()-removes and returns the last element
  44.  
  45. сортиране на масив
  46. var numbers = [5, 4, 23, 2];
  47. numbers.sort(function(a, b) {
  48. return a > b;
  49. });
  50. console.log(numbers.join(', ')); // returns 2, 4, 5, 23
  51.  
  52. array.reverse()-връща нов масив с елементите в обратен ред
  53. arr.slice(start,end-1)-връща елементите на масива от start позиция до end позиция
  54. array.concat(elements)-прилепя елементи в края на масива
  55. array.join(separator)-прилепя елементите на масива с конкретен елемент
  56.  
  57. array.filter(function(item){ return true / false })
  58. Returns a new array with the elements that satisfy condition
  59.  
  60. array.forEach(function(item){ … })
  61. Iterates through the array and executes the function for each item
  62.  
  63. array.indexOf(element)
  64. Returns the index of the first match in the array
  65. Returns -1 if the element is not found
  66.  
  67. array.lastIndexOf(element)
  68. Returns the index of the first match in the array from right to left or -1 (not found)
  69.  
  70. Асоциативнни масиви:
  71.  Associative arrays are arrays indexed by keys, not by the numbers 0, 1, 2, 3, …
  72.  
  73.  Initializing an associative array (object):
  74. var prices = { 'orange' : 2.30, 'apple' : 1.50, 'tomato' : 3.80 };
  75.  Taking the keys of object / array:
  76.  
  77. var prices = { 'orange' : 2.30, 'apple' : 1.50, 'tomato' : 3.80 };
  78. console.log(Object.keys(prices)); // ["orange", "apple", "tomato"]
  79.  
  80. var nums = [10, 20, 30];
  81. console.log(Object.keys(nums)); // ["0", "1", "2"]
  82.  
  83. в JavaScript обектите са напрактика асоциативни масиви, но с тази разлика, че обектите нямат дължина
  84.  
  85. Strings
  86.  
  87. String.search(regex(регулярен израз))—спрямо регулярния израз получаваме началние индекс на 1вото срещане на string-а ,който търсим
  88.  
  89. Конкатенацията е бърз процес в JavaScript
  90.  
  91. !когато пишем в HTML ако искаме да напишем даден таг без да искаме да се рендира трябва да escape-ваме самите символи
  92.  
  93. Conversion from primitive to object type:
  94.  new String('…') creates a string object
  95.  String(strObject) creates a primitive string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement