天天看点

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

继续阅读