理解javascript腳本語言中運算符(operator)的類型轉換(譯)

 

var x = 99;   = 是賦值運算符,用於對變量進行賦值。

x == 99; == 是比較運算符,用於比較兩個變量是否相等

 

相等運算符(equality operator)==和嚴格相等運算符(strict equality/identity operator)

使用嚴格相等運算符,要保證變量是同一個數據類型。

If the two values have the same type, just compare them

If the two values have different types,  try to convert them into the same type and then compare them

 

CASE#1: 比較數字和字符串

字符串轉成數字:the string is converted into a number, and the two numbers are then compared.

假如字符串沒辦法轉數字,就會自動轉成NaN,結果爲不相等

 

CASE#2: 比較boolean和其他類型

we convert the boolean to a number, and compare. This might seem a little strange, but it’s easier to digest if you just remember that true converts to 1 and false converts to 0.

布爾值和其他類型進行比較會自動轉換成數字,true是1,false是0

布爾值和字符串進行比較,字符串也會轉成數字

 

CASE#3: 比較null 和 undefined.

Comparing these values evalutates to true.  That might seem odd as well, but it’s the rule. For some insight, these values both essentially represent “no value” (that is, a variable with no value, or an object with no value), so they are considered to be equal. undefined == null Undefined and null are always equal. true

null和undefined其實都表示沒有值

 

CASE#4: 需要特別注意

a 對象的比較

 

b 還有 空字符串在js中會自動轉換成0

 

 

還有“true”==true是不成立的,過程如下:

首先是布爾值和其他類型進行比較,所以會首先把true轉成1,再將“true”和1 進行比較

字符串和數字進行比較,會嘗試將“true”轉換爲數字,但是結果以失敗告終,返回的結果爲false

 

兩個字符串如何比較大小呢?

按其在字母表中的順序排列

“banana”<"mango"因爲m排的順序更後,所以更大

“mango”<"melon"因爲e排的比a順序更後

“Mango”<"mango",小寫字母在Unicode裏面值大於大寫,小寫比大寫排的更後

 

 

還有>=和<=

These operators only know how to compare strings and numbers , so if you try to compare any values other than two strings or two numbers (or a string and a number), JavaScript will attempt to convert the types using the rules we’ve discussed.

 

true>=false成立,返回爲true,但是這種比較沒有意義.  

99<= "100"成立,返回爲true,字符串“100”會自動轉爲數字

 

 

 

嚴格相等===就是需要兩個值的類型和值都相等,如果兩個值類型不等,結果都會是false

Two values are strictly equal only if they have the same type and the same value.

相對應的還有嚴格不等

!== is stricter than !=. You use the same rules for !== as you do for ===, except that you’re checking for inequality instead of equality

 

在比較運算符<和>中,也會運用同樣的規則,

比如0<true是成立的,true會被轉成數字

 

 

 

conversions

 

arithmetic operators

"+": addition, concatenation; 數字和字符串相加的時候,數字會轉爲字符串(與==不同),只要有操作數爲字符串,就都會視爲字符串拼接

 

不過+的結合性是從左到右的,所以假如兩個數字在左邊,還是會先運行加法

 

而其他的arithmetic operators。會默認是運算,把字符串轉成數字

When it comes the other arithmetic operators—like multiplication, division and subtraction— JavaScript prefers to treat those as arithmetic operations, not string operations.

 

 

 

還有一些特殊的情況也會造成類型轉換

比如-true會是-1,true+“love”結果會是true love(布爾值與字符串相加,結果爲字符串)

但是這種類型轉換比較無聊,代碼不會見到

 

要指定字符串轉換爲數字,還有一個函數,名爲Number

var num= 3+ Number("4");

4就會被轉換爲數字,假如指定的實參無法轉換爲數字,number將返回NaN

 

 

 

判斷對象的相等,比較的是指向對象的引用

When we test equality of two object variables, we compare the references to those objects

if both operands are objects, then you can use either == or === because they work in exactly the same way. 

Two references are equal only if they reference the same object

僅當兩個變量包含的對象引用指向同一個對象時,纔會相等

 

 

 

 

假值(truth)和真值(falsey)

To remember which values are truthy and which are falsey, just memorize the five falsey values— undefined, null, 0, "” and NaN—and remember that everything else is truthy.

這些都會返回false

 

undefined is falsey.

null is falsey.

0 is falsey.

The empty string is falsey.

NaN is falsey.

 

因此null==undefined返回爲true,因爲他們都是假值

 

所以剩下的都會返回true;

 

 

 

 

type一定會是基本類型(primitive)或者對象(不是基本類型就一定是對象)

 

Types always belong to one of two camps: they’re either a primitive type or an object. Primitives live out fairly simple lives, while objects keep state and have behavior (or said another way, have properties and methods).

 

但是字符串string的type表現得既像基本類型(number,Boolean,string),又像對象

with JavaScript you can create a string that is a primitive, and you can also create one that is an object (which supports lots of useful string manipulation methods). Now, we’ve never talked about how to create a string that is an object, and in most cases you don’t need to explicitly do it yourself, because the JavaScript interpreter will create string objects for you, as needed.

 

字符串的property和method

The length propertyholds the number of characters in the string. It’s quite handy for iterating through the characters of the string

The charAt methodtakes an integer number between zero and the length of the string (minus one), and returns a string containing the single character at that position of the string. Think of the string a bit like an array, with each character at an index of the string, with the indices starting at 0 (just like an array). If you give it an index that is greater than or equal to the length of the string, it returns the empty string

 

The indexOf methodtakes a string as an argument and returns the index of the first character of the first occurrence of that argument in the string.

Give the substring methodtwo indices, and it will extract and return the string contained within them

The split methodtakes a character that acts as a delimiter, and breaks the string into parts based on the delimiter

 

some other methods

slice: Returns a new string that has part of the original string removed

trim: Removes whitespace from around the string. Handy when processing user input.

match: Searches for matches in a string using regular expressions

lastIndexOf: Just  like indexOf, but finds the last, not the first, occurrence.

replace: Finds substrings and replaces them with another string.

concat: Joins strings together.

 

原文來自head first javascript programing

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章