天天看點

了解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

繼續閱讀