|
894 | 894 |
|
895 | 895 |
|
896 | 896 |
|
897 |
| -6. <a name="naming">Naming</a> |
| 897 | +6. <a name="naming">Наименуване</a> |
898 | 898 |
|
899 | 899 |
|
900 | 900 |
|
901 |
| - A. You are not a human code compiler/compressor, so don't try to be one. |
| 901 | + A. Вие не сте човешки компилатор/компресор на код, затова не се опитвайте да бъдете такъв. |
902 | 902 |
|
903 |
| - The following code is an example of egregious naming: |
| 903 | + Последващия код е пример за лошо именуване: |
904 | 904 |
|
905 | 905 | ```javascript
|
906 | 906 |
|
907 | 907 | // 6.A.1.1
|
908 |
| - // Example of code with poor names |
| 908 | + // Пример за код с лоши имена |
909 | 909 |
|
910 | 910 | function q(s) {
|
911 | 911 | return document.querySelectorAll(s);
|
|
914 | 914 | for(i=0;i<els.length;i++){a.push(els[i]);}
|
915 | 915 | ```
|
916 | 916 |
|
917 |
| - Without a doubt, you've written code like this - hopefully that ends today. |
918 |
| -
|
919 |
| - Here's the same piece of logic, but with kinder, more thoughtful naming (and a readable structure): |
| 917 | + Без съмнение сте писали код като този - дано това приключи днес. |
920 | 918 |
|
| 919 | + Сега същото парче логика, но с по-добро, по-смислено именуване (и четима структура): |
| 920 | + |
921 | 921 | ```javascript
|
922 | 922 |
|
923 | 923 | // 6.A.2.1
|
924 |
| - // Example of code with improved names |
| 924 | + // Пример на кода с подобрени имена |
925 | 925 |
|
926 | 926 | function query( selector ) {
|
927 | 927 | return document.querySelectorAll( selector );
|
|
938 | 938 |
|
939 | 939 | ```
|
940 | 940 |
|
941 |
| - A few additional naming pointers: |
| 941 | + Още няколко съвета за именуване на променливи:: |
942 | 942 |
|
943 | 943 | ```javascript
|
944 | 944 |
|
945 | 945 | // 6.A.3.1
|
946 |
| - // Naming strings |
| 946 | + // Именуване на низове |
947 | 947 |
|
948 | 948 | `dog` is a string
|
949 | 949 |
|
950 | 950 |
|
951 | 951 | // 6.A.3.2
|
952 |
| - // Naming arrays |
| 952 | + // Именуване на масиви |
953 | 953 |
|
954 | 954 | `dogs` is an array of `dog` strings
|
955 | 955 |
|
956 | 956 |
|
957 | 957 | // 6.A.3.3
|
958 |
| - // Naming functions, objects, instances, etc |
| 958 | + // Именуване на фукнции, обекти, инстанции, и т.н. |
959 | 959 |
|
960 | 960 | camelCase; function and var declarations
|
961 | 961 |
|
962 | 962 |
|
963 | 963 | // 6.A.3.4
|
964 |
| - // Naming constructors, prototypes, etc. |
965 |
| -
|
| 964 | + // Именуване на конструктори, прототипи и т.н. |
966 | 965 | PascalCase; constructor function
|
967 | 966 |
|
968 | 967 |
|
969 | 968 | // 6.A.3.5
|
970 |
| - // Naming regular expressions |
| 969 | + // Именуване на регулярни изрази |
971 | 970 |
|
972 | 971 | rDesc = //;
|
973 | 972 |
|
974 | 973 |
|
975 | 974 | // 6.A.3.6
|
976 |
| - // From the Google Closure Library Style Guide |
| 975 | + // Из Ръководство по стила Google Closure Library |
977 | 976 |
|
978 | 977 | functionNamesLikeThis;
|
979 | 978 | variableNamesLikeThis;
|
|
984 | 983 |
|
985 | 984 | ```
|
986 | 985 |
|
987 |
| - B. Faces of `this` |
988 |
| - |
989 |
| - Beyond the generally well known use cases of `call` and `apply`, always prefer `.bind( this )` or a functional equivalent, for creating `BoundFunction` definitions for later invocation. Only resort to aliasing when no preferable option is available. |
| 986 | + B. Лица на `this` |
990 | 987 |
|
| 988 | + Извън общоизвестните случаи на `call` и `apply`, винаги избирайте `.bind( this )` или фунционалния му еквивалент, за създаване на дефиниция `BoundFunction` за по-късно извикване. Създаването на псевдоним е в краян случай, ако други решения не са подходящи. |
| 989 | + |
991 | 990 | ```javascript
|
992 | 991 |
|
993 | 992 | // 6.B.1
|
994 | 993 | function Device( opts ) {
|
995 | 994 |
|
996 | 995 | this.value = null;
|
997 | 996 |
|
998 |
| - // open an async stream, |
999 |
| - // this will be called continuously |
| 997 | + // Отворете асинхронен поток, |
| 998 | + // това ще се извиква продължително |
1000 | 999 | stream.read( opts.path, function( data ) {
|
1001 | 1000 |
|
1002 |
| - // Update this instance's current value |
1003 |
| - // with the most recent value from the |
1004 |
| - // data stream |
| 1001 | + // Обновете текущата стойност на инстанцията |
| 1002 | + // с последната стойност от |
| 1003 | + // потока на данни |
1005 | 1004 | this.value = data;
|
1006 | 1005 |
|
1007 | 1006 | }.bind(this) );
|
1008 | 1007 |
|
1009 | 1008 | // Throttle the frequency of events emitted from
|
1010 | 1009 | // this Device instance
|
| 1010 | + // Ограничете честотата на събитията изпратени от |
| 1011 | + // инстанцията на Device |
1011 | 1012 | setInterval(function() {
|
1012 | 1013 |
|
1013 |
| - // Emit a throttled event |
| 1014 | + // Изпращане на събитие |
1014 | 1015 | this.emit("event");
|
1015 | 1016 |
|
1016 | 1017 | }.bind(this), opts.freq || 100 );
|
1017 | 1018 | }
|
1018 | 1019 |
|
1019 |
| - // Just pretend we've inherited EventEmitter ;) |
| 1020 | + // Просто се преструвайте, че сме наследили EventEmitter ;) |
1020 | 1021 |
|
1021 | 1022 | ```
|
1022 | 1023 |
|
1023 |
| - When unavailable, functional equivalents to `.bind` exist in many modern JavaScript libraries. |
| 1024 | + Когато не е достъпен фукнционалния еквивалент на `.bind` съществува в много модерни JavaScript библиотеки. |
1024 | 1025 |
|
1025 | 1026 |
|
1026 | 1027 | ```javascript
|
1027 | 1028 | // 6.B.2
|
1028 | 1029 |
|
1029 |
| - // eg. lodash/underscore, _.bind() |
| 1030 | + // например lodash/underscore, _.bind() |
1030 | 1031 | function Device( opts ) {
|
1031 | 1032 |
|
1032 | 1033 | this.value = null;
|
|
1044 | 1045 | }, this), opts.freq || 100 );
|
1045 | 1046 | }
|
1046 | 1047 |
|
1047 |
| - // eg. jQuery.proxy |
| 1048 | + // например jQuery.proxy |
1048 | 1049 | function Device( opts ) {
|
1049 | 1050 |
|
1050 | 1051 | this.value = null;
|
|
1062 | 1063 | }, this), opts.freq || 100 );
|
1063 | 1064 | }
|
1064 | 1065 |
|
1065 |
| - // eg. dojo.hitch |
| 1066 | + // например dojo.hitch |
1066 | 1067 | function Device( opts ) {
|
1067 | 1068 |
|
1068 | 1069 | this.value = null;
|
|
1082 | 1083 |
|
1083 | 1084 | ```
|
1084 | 1085 |
|
1085 |
| - As a last resort, create an alias to `this` using `self` as an Identifier. This is extremely bug prone and should be avoided whenever possible. |
| 1086 | + В краен случай, създайте алиас на `this` използвайки `self` като идентификатор. Това е изключително податливо на грешки и трябва да се избягва, когато е възможно. |
1086 | 1087 |
|
1087 | 1088 | ```javascript
|
1088 | 1089 |
|
|
1109 | 1110 | ```
|
1110 | 1111 |
|
1111 | 1112 |
|
1112 |
| - C. Use `thisArg` |
| 1113 | + C. Използване на `thisArg` |
1113 | 1114 |
|
1114 |
| - Several prototype methods of ES 5.1 built-ins come with a special `thisArg` signature, which should be used whenever possible |
| 1115 | + Няколко прототипни метода вградени в ES 5.1 идват със специалния `thisArg`, който трябва да се използва когато е възможно |
1115 | 1116 |
|
1116 | 1117 | ```javascript
|
1117 | 1118 |
|
|
1123 | 1124 |
|
1124 | 1125 | Object.keys( obj ).forEach(function( key ) {
|
1125 | 1126 |
|
1126 |
| - // |this| now refers to `obj` |
| 1127 | + // |this| сочи към `obj` |
1127 | 1128 |
|
1128 | 1129 | console.log( this[ key ] );
|
1129 | 1130 |
|
1130 |
| - }, obj ); // <-- the last arg is `thisArg` |
| 1131 | + }, obj ); // <-- последния аргумент е `thisArg` |
1131 | 1132 |
|
1132 |
| - // Prints... |
| 1133 | + // Ще изпечата... |
1133 | 1134 |
|
1134 | 1135 | // "foo"
|
1135 | 1136 | // "bar"
|
1136 | 1137 | // "qux"
|
1137 | 1138 |
|
1138 | 1139 | ```
|
1139 | 1140 |
|
1140 |
| - `thisArg` can be used with `Array.prototype.every`, `Array.prototype.forEach`, `Array.prototype.some`, `Array.prototype.map`, `Array.prototype.filter` |
| 1141 | + `thisArg` може да се използва `Array.prototype.every`, `Array.prototype.forEach`, `Array.prototype.some`, `Array.prototype.map`, `Array.prototype.filter` |
1141 | 1142 |
|
1142 | 1143 | 7. <a name="misc">Misc</a>
|
1143 | 1144 |
|
|
0 commit comments