原始数据

6种基本类型: string, number, boolean, null, undefined, symbol(es6)

string

1
2
3
var str = "str";
str.toUpperCase();
console.log(str); // "str"

dir

1
1 === 1.0 // true

引用类型

引用类型通常叫做类(Class)

  1. Object
    • 属性:
      • constructor 对创建对象的函数的引用。对于Object对象,该指针指向原始的Object()函数
      • Prototype 对该对象的对象原型的引用。
    • hasOwnProperty(property)
    • isPrototypeOf(obj) 判断该对象是否为另一个对象的原型
    • PropertyIsEnumerable(property) 判断给定的属性是否为另一个对象的原型
    • Oject.valueOf(); 返回对象的原始值
  2. Boolean 对象
  3. Number 对象
    • toFixed(fixed)
  4. String 对象
    • length
    • charAt(index) 返回index位置字符
    • charCodeAt(index) 返回index位置字符的ASCII码
    • concat()
    • indexOf()
    • lastIndexOf()
    • localeCompare()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//如果 String 对象按照字母顺序排在参数中的字符串之前,返回负数。
//如果 String 对象等于参数中的字符串,返回 0
//如果 String 对象按照字母顺序排在参数中的字符串之后,返回正数。

var oStringObject = new String("yellow");
alert(oStringObject.localeCompare("brick")); //输出 "1"
alert(oStringObject.localeCompare("yellow")); //输出 "0"
alert(oStringObject.localeCompare("zoo")); //输出 "-1"

- slice( Number firstIndex, Number length) 如果参数length无值,则默认获取到最后
- substring(Number firstIndex, Number length) 如果参数length无值,则默认获取到最后
- toLowerCase(),toLocalLoweCase(),toUpperCase(),toLocaleUpperCase() 不过,有几种语言对 Unicode 大小写转换应用了特定的规则(例如土耳其语),因此必须使用区域特定的方法才能进行正确的转换。
### instanceof
```js
"string" instanceof String // false
new String("string") instanceof String //true

类型转换(Boolean值、数字、和字符串的原始值)

ECMAScript的Boolean值、数字、和字符串的原始值的有趣的地方在于他们是伪对象。

转换成字符串

- toString() 将原始值转为字符串。
- Number.toString()
1
2
3
4
5
6
7
8
9
var iNum1 = 10;
var iNum2 = 10.0;
alert(iNum1.toString()); //输出 "10"
alert(iNum2.toString()); //输出 "10"

var iNum = 10;
alert(iNum.toString(2)); //输出 "1010"
alert(iNum.toString(8)); //输出 "12"
alert(iNum.toString(16)); //输出 "A"

转换成数字

  • parseInt(原始值[,基值])
1
2
3
4
5
6
var iNum1 = parseInt("12345red");	//返回 12345
var iNum1 = parseInt("0xA"); //返回 10
var iNum1 = parseInt("56.9"); //返回 56
var iNum1 = parseInt("red"); //返回 NaN

var iNum1 = parseInt("AF", 16);
  • parseFloat(原始值[,基值])
1
2
3
4
5
6
var fNum1 = parseFloat("12345red");	//返回 12345
var fNum2 = parseFloat("0xA"); //返回 NaN
var fNum3 = parseFloat("11.2"); //返回 11.2
var fNum4 = parseFloat("11.22.33"); //返回 11.22
var fNum5 = parseFloat("0102"); //返回 102
var fNum1 = parseFloat("red"); //返回 NaN

隐式转换

转换规则(js中,不通类型之间进行四则运算的时候,常常会触发js的隐式类型转换机制)

  1. console.log() // 通常会将任何值转换成字符串然后展示
  2. 数学运算则会首先将值转化为数值类型
1
2
3
4
5
6
7
8
9
10
11
12
13
// compare 
[] == ![]; // true
NaN !== NaN; // true

1 == true // true
2 == true // false
"2" == true // false

null > 0 // false
null < 0 // false
null == 0 //flase
null >= 0 // true
null <= 0 // true

当js需要一个boolean值时,例如if( 表达式 ){},表达式的返回值会被强制转换为true或者false。

下面值会被转换为 false:

  • undefined, null
  • Boolean: false
  • Number: -0,+0,NaN
  • String:””

字符串的隐式转换

1
2
var x = '5';  // 错误的假设:x 是一个数字
x + 1; //'51'

对象的隐式转换

  1. 调用 valueOf()。如果结果是原始值(不是一个对象),则将其转换为一个数字。
  2. 否则,调用 toString() 方法。如果结果是原始值,则将其转换为一个数字。
  3. 否则,抛出一个类型错误。
1
2
3 * { valueOf: function () { return 5 } } // 15
{}+{} //"[object Object][object Object]"

== 和 ===

typeof

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16


typeof true; //"boolean"
typeof Boolean(true); //"boolean"
typeof new Boolean(true); //"object"
typeof (new Boolean(true)).valueOf(); //"boolean"

typeof "abc"; //"string"
typeof String("abc"); //"string"
typeof new String("abc"); //"object"
typeof (new String("abc")).valueOf(); //"string"

typeof 123; //"number"
typeof Number(123); //"number"
typeof new Number(123); //"object"
typeof (new Number(123)).valueOf(); //"number"

new Function() 的实例为 Object 类型

1
2
3
4
5
6
7
8
9
10
11
String.prototype.returnMe= function() {
return this;
}

var a = "abc";
var b = a.returnMe();

a; //"abc"
typeof a; //"string" (still a primitive)
b; //"abc"
typeof b; //"object"
1
2
3
4
5
6
//object coerced to primitive 
var Twelve = new Number(12);
var fifteen = Twelve + 3;
fifteen; //15
typeof fifteen; //"number" (primitive)
typeof Twelve; //"object"; (still object)
1
2
3
if (new Boolean(false)) {
alert("true???");
}

基值不能assign值

var primitive = "september";
primitive.vowels = 3;

primitive.vowels; //undefined;

new Function() 创建的基值唯一的优势就是能assgin values,但是有一些其它方面的混淆。