|
| 1 | +<html> |
| 2 | +<head> |
| 3 | +<title>The JavaScript Encyclopedia: B</title> |
| 4 | +<link rel="stylesheet" href="encyclopedia.css" type="text/css"> |
| 5 | +</head> |
| 6 | + |
| 7 | +<body> |
| 8 | +<h1>B</h1> |
| 9 | + |
| 10 | +<h2 id="bind">bind</h2> |
| 11 | +<h3 id="bind function prototype function"><code>bind</code> <code>function</code> prototype function</h3> |
| 12 | +<p>Blah.</p> |
| 13 | +<p class="es3">The <code>bind</code> function is not available in <a href="E.html#ES3">ES3</a>.</p> |
| 14 | + |
| 15 | +<h2 id="bitwise operator">bitwise operator</h2> |
| 16 | +<p>A bitwise operator treats a <a href="N.html#number">number</a> as a 32 bit 2's complement signed integer. The bitwise operators |
| 17 | + are</p> |
| 18 | +<ul> |
| 19 | + <li><a href="special.html#ampersand%20infix%20operator"><code>&</code> infix |
| 20 | + operator</a> <dfn>bitwise and</dfn></li> |
| 21 | + <li><a href="special.html#bar%20infix%20operator"><code>|</code> infix operator</a> |
| 22 | + <dfn>bitwise or</dfn></li> |
| 23 | + <li><a href="special.html#caret%20infix%20operator"><code>^</code> infix operator</a> |
| 24 | + <dfn>bitwise exclusive or</dfn></li> |
| 25 | + <li><a href="special.html#ampersandlt;<%20infix%20operator"><code><<</code> |
| 26 | + infix operator</a> <dfn>bitwise left shift</dfn></li> |
| 27 | + <li><a href="special.html#greater>%20infix%20operator"><code>>></code> |
| 28 | + infix operator</a> <dfn>bitwise signed right shift</dfn></li> |
| 29 | + <li><a href="special.html#greater>>%20infix%20operator"><code>>>></code> |
| 30 | + infix operator</a> <dfn>bitwise unsigned right shift</dfn></li> |
| 31 | + <li><a href="special.html#tilde%20infix%20operator"><code>~</code> prefix |
| 32 | + operator</a> <dfn>bitwise not</dfn></li> |
| 33 | +</ul> |
| 34 | +<p>These operators allow access to some of the primitive operations of CPUs. They all treat <a href="N.html#NaN"><code>NaN</code></a>, <a href="N.html#null"><code>null</code></a>, and <a href="U.html#undefined"><code>undefined</code></a> as <code>0</code>. Also see <a href="I.html#integer">integer</a>.</p> |
| 35 | + |
| 36 | +<p>Example:</p> |
| 37 | +<pre>var raw_base64 = (function (alphabet) { |
| 38 | + |
| 39 | +// The raw_base64 object contains two methods, encode and decode, that convert |
| 40 | +// strings to base64 and back, as specified in Multipurpose Internet Mail |
| 41 | +// Extensions (MIME) Section 6.8. URL: http://www.ietf.org/rfc/rfc2045.txt. |
| 42 | +// The standard also requires padding with = and the breaking and joining of |
| 43 | +// lines no longer than 76 characters. We are not concerned with that. All |
| 44 | +// we are doing here is encoding groups of 3 8-bit characters as 4 7-bit |
| 45 | +// characters. |
| 46 | + |
| 47 | +// The original motivation for doing this was to avoid data interchange |
| 48 | +// problems caused by poor architectural choices in some mid 20th century |
| 49 | +// operating systems. These workarounds became standards that are still in |
| 50 | +// force, decades after solution of the root problem. Once something gets into |
| 51 | +// a standard, it can take generations to get rid of it. |
| 52 | + |
| 53 | +// We will encode using an alphabet of 64 characters: 26 upper case letters, |
| 54 | +// 26 lower case letters, 10 digits, and two special characters. For efficent |
| 55 | +// decoding, we will invert the alphabet ('ABC...9+/') into an object, producing |
| 56 | +// { |
| 57 | +// A: 0, |
| 58 | +// B: 1, |
| 59 | +// C: 2, |
| 60 | +// ... |
| 61 | +// '9': 61, |
| 62 | +// '+': 62, |
| 63 | +// '/': 63 |
| 64 | +// } |
| 65 | + |
| 66 | + var alphabet = |
| 67 | + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', |
| 68 | + tebahpla = {}; |
| 69 | + alphabet.split('').forEach(function (value, index) { |
| 70 | + tebahpla[value] = index; |
| 71 | + }); |
| 72 | + |
| 73 | +// Return the object containing the encode and decode methods. |
| 74 | + |
| 75 | + return { |
| 76 | + encode: function (unencoded) { |
| 77 | + var i, |
| 78 | + length = unencoded.length, |
| 79 | + result = [], |
| 80 | + in0, |
| 81 | + in1, |
| 82 | + in2; |
| 83 | + |
| 84 | + for (i = 0; i < length; i += 3) { |
| 85 | + |
| 86 | +// Get the next three character codes. |
| 87 | + |
| 88 | + in0 = unencoded.charCodeAt(i); |
| 89 | + in1 = unencoded.charCodeAt(i + 1); |
| 90 | + in2 = unencoded.charCodeAt(i + 2); |
| 91 | + |
| 92 | +// Redistribute 3 sets of 8 bits into 4 sets of 6 bits. |
| 93 | + |
| 94 | +// in: abcdefgh ijklmnop qrstuvwx |
| 95 | +// out: 00abcdef 00ghijkl 00mnopqr 00stuvwx |
| 96 | + |
| 97 | +// This involves shifting bits into a new positions, using & (and) to mask off |
| 98 | +// some bits, and using | (or) to combine. |
| 99 | + |
| 100 | +// Translate the 6-bit values into symbols of the alphabet and push them into |
| 101 | +// the results. |
| 102 | + |
| 103 | + result.push(alphabet[in0 >> 2]); |
| 104 | + result.push(alphabet[((in0 & 3) << 4) | (in1 >> 4)]); |
| 105 | + |
| 106 | +// We only output the third character if in1 was present. |
| 107 | + |
| 108 | + if (!isNaN(in1)) { |
| 109 | + result.push(alphabet[((in1 & 15) << 2) | (in2 >> 6)]); |
| 110 | + |
| 111 | +// And we only output the fourth character if in2 was present. |
| 112 | + |
| 113 | + if (!isNaN(in2)) { |
| 114 | + result.push(alphabet[in2 & 63]); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | +// Return the result string. |
| 120 | + |
| 121 | + return result.join(''); |
| 122 | + }, |
| 123 | + |
| 124 | + decode: function (encoded) { |
| 125 | + var i, |
| 126 | + length = encoded.length - 1, |
| 127 | + result = [], |
| 128 | + in0, |
| 129 | + in1, |
| 130 | + in2, |
| 131 | + in3; |
| 132 | + for (i = 0; i < length; i += 4) { |
| 133 | + |
| 134 | +// Get the next four character codes. |
| 135 | + |
| 136 | + in0 = tebahpla[encoded.charAt(i)]; |
| 137 | + in1 = tebahpla[encoded.charAt(i + 1)]; |
| 138 | + in2 = tebahpla[encoded.charAt(i + 2)]; |
| 139 | + in3 = tebahpla[encoded.charAt(i + 3)]; |
| 140 | + |
| 141 | +// Redistribute 4 sets of 6 bits into 3 sets of 8 bits. |
| 142 | + |
| 143 | +// in: 00abcdef 00ghijkl 00mnopqr 00stuvwx |
| 144 | +// out: abcdefgh ijklmnop qrstuvwx |
| 145 | + |
| 146 | +// Combine bits from in0 and in1 to produce the first character code. |
| 147 | +// Convert the code into a character, and push it onto the result. |
| 148 | + |
| 149 | + result.push(String.fromCharCode((in0 << 2) | (in1 >> 4))); |
| 150 | + |
| 151 | +// We only output the second character if in2 was present. |
| 152 | + |
| 153 | + if (in2 !== undefined) { |
| 154 | + result.push(String.fromCharCode(((in1 & 15) << 4) | (in2 >> 2))); |
| 155 | + |
| 156 | +// And we only output the third character if in3 was present. |
| 157 | + |
| 158 | + if (in3 !== undefined) { |
| 159 | + result.push(String.fromCharCode(((in2 & 3) << 6) | in3)); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | +// Return the result string. |
| 165 | + |
| 166 | + return result.join(''); |
| 167 | + } |
| 168 | + }; |
| 169 | +}());</pre> |
| 170 | +<h2 id="blink">blink</h2> |
| 171 | +<p><blink>Blah.</blink></p> |
| 172 | +<h2 id="block">block</h2> |
| 173 | +<p>A block is a compound <a href="S.html#statement">statement</a> that can occur anywhere that a <a href="S.html#statement">statement</a> can occur. It begins with a <a href="special.html#leftbrace"><code>{</code> <dfn>left curly brace</dfn></a> and ends with a <a href="special.html#}"><code>}</code> <dfn>right curly brace</dfn></a>, and between the two curly braces there are zero or more statements. Blocks are commonly used as parts of other statements, such as <a href="I.html#if statement"><code>if</code> statement</a> and <a href="W.html#while statement"><code>while</code> statement</a>.</p> |
| 174 | +<p>A brace does not create a new <a href="S.html#scope">scope</a>. Only a function has <a href="S.html#scope">scope</a>. In many other languages, a block does create a <a href="S.html#scope">scope</a>, so that a <a href="V.html#variable">variable</a> that is declared inside of a block is not visible outside of the block. That does not happen in the language, which is why it is unwise to declare a <a href="variable">variable</a> in a block.</p> |
| 175 | +<h2>body</h2> |
| 176 | +<p>See <a href="F.html#function body">function body</a>.</p> |
| 177 | + |
| 178 | +<h2 id="boolean">boolean</h2> |
| 179 | +<p>The boolean type is named after the mathematician George Boole. The boolean type contains exactly two values: <a href="T.html#true"><code>true</code></a> and <a href="F.html#false"><code>false</code></a>. The <a href="T.html#typeof prefix operator"><code>typeof</code> prefix operator</a> returns <code>'boolean'</code> when its operand is a boolean. A boolean value is produced by a <a href="R.html#relational operator">relational operator</a>.</p> |
| 180 | +<h3 id="Boolean global function"><code>Boolean</code> global function</h3> |
| 181 | +<p>The <code>Boolean(</code><var>value</var><code>)</code> <a href="G.html#global function">global function</a>, when called as function, evaluates its parameter. If the value was <a href="T.html#truthy">truthy</a>, it returns <a href="T.html#true"><code>true</code></a>. If the value was <a href="F.html#falsy">falsy</a>, it returns <a href="F.html#false"><code>false</code></a>. </p> |
| 182 | +<h3 id="boolean literal">boolean literal</h3> |
| 183 | +<p>There are two boolean literals, representing the two boolean values: <a href="T.html#true"><code>true</code></a> and <a href="F.html#false"><code>false</code></a>.</p> |
| 184 | +<h2 id="boolish">boolish</h2> |
| 185 | +<p>Every value in JavaScript is boolish in that it is either <a href="T.html#truthy">truthy</a> or <a href="F.html#falsy">falsy</a>. The <a href="#boolean">boolean</a> type contains only the <code>true</code> and <code>false</code> values, but the boolish type includes all values.</p> |
| 186 | +<h2 id="bound variable">bound variable</h2> |
| 187 | +<p>Blah.</p> |
| 188 | +<h2>brace</h2> |
| 189 | +<p>See <a href="special.html#leftbrace"><code>{</code> <dfn>left curly brace</dfn></a> and <a href="special.html#}"><code>}</code> <dfn>right curly brace</dfn></a>.</p> |
| 190 | +<h2>bracket</h2> |
| 191 | +<p>See <a href="special.html#leftbracket"><code>[</code> <dfn>left bracket |
| 192 | +</dfn></a> and <a href="special.html#rightbracket"><code>]</code> <dfn>right bracket</dfn></a>.</p> |
| 193 | + |
| 194 | +<h2 id="break">break <a href="R.html#reserved word"><strong>reserved word</strong></a></h2> |
| 195 | +<h3 id="break statement"><code>break</code> statement</h3> |
| 196 | +<p>Blah.</p> |
| 197 | +</body> |
| 198 | +</html> |
0 commit comments