tailieunhanh - JQuery: Novice to Ninja- P28
JQuery: Novice to Ninja- P28:No matter what kind of ninja you are—a cooking ninja, a corporate lawyer ninja, or an actual ninja ninja—virtuosity lies in first mastering the basic tools of the trade. Once conquered, it’s then up to the full-fledged ninja to apply that knowledge in creative and inventive ways. | 382 jQuery Novice to Ninja When we add a string and a number using the operator JavaScript assumes we re trying to concatenate the two so it creates a new string. It would appear to change the number s variable type to string. When we use the multiplication operator though JavaScript assumes that we want to treat the two variables as numbers. The variable itself remains the same throughout it s just treated differently. We can always explicitly tell JavaScript how we intend to treat a variable but if we don t we need to understand just what JavaScript is doing for us. Here s another example alert a c alerts 22 alert a parselnt c alerts 4 Equality Operators The equal sign and its related operators can also provide a trap for young players. And where it was once just a little odd it became even more so in JavaScript . The first trick is that the equal sign has a different meaning than what you remember from your school mathematics classes var a 2 var b 2 A single equal sign is an assignment operator and is used to assign values to variables. We all knew what it did but now we know what it s called var c a b Two signs together is known as the equality operator and establishes a Boolean value. In our example the variable c will have a value of true as JavaScript compares the values before and after the equality operator and considers them to be equal. Using the equality operator JavaScript pays no heed to the variable s type and attempts to coerce the values to assess them. Switch out the first equal sign for an exclamation mark and you have yourself an inequality operator . This operator will return false if the variables are equal or true if they aren t Appendix B JavaScript Tidbits 383 var d a b The variable d will now have a value of false since a and b are equal. It may be a little complex but at least it s consistent. In JavaScript the situation became less simple still with the introduction of one further operator the strict equality operator shown as . .
đang nạp các trang xem trước