JavaScript权威指南学习笔记(一)2

来源:互联网 发布:廖雪峰python教程mobi 编辑:程序博客网 时间:2024/05/20 16:09

第3章 类型、值和变量

文本

字符串的使用

var s = "hello, world"      // Start with some text.s.charAt(0)                 // => "h": the first character.s.charAt(s.length-1)        // => "d": the last character.s.substring(1,4)            // => "ell": the 2nd, 3rd and 4th characters.s.substr(1,4)               // => "ello",1 is the first, 4 is the lengths.slice(1,4)                // => "ell": same things.slice(-3)                 // => "rld": last 3 characterss.indexOf("l")              // => 2: position of first letter l.s.lastIndexOf("l")          // => 10: position of last letter l.s.indexOf("l", 3)           // => 3: position of first "l" at or after 3s.split(", ")               // => ["hello", "world"] split into substringss.replace("h", "H")         // => "Hello, world": replaces all instancess.toUpperCase()             // => "HELLO, WORLD"
  • JavaScript中字符串是不会改变的,类似replace() 和toUpperCase()的方法都返回新字符串。原字符串本身没有发生变化。
  • 在ECMAScript5中,字符串可以作为只读数组,除了使用charAt()方法,还可以使用方括号来访问字符串中的单个字符
s = "hello, world";s[0] // => "h"s[s.length-1] // => "d"

日期和时间

var then = new Date(2010, 0, 1); // The 1st day of the 1st month of 2010var later = new Date(2010, 0, 1, // Same day, at 5:10:30pm, local time17, 10, 30);var now = new Date();           // The current date and timevar elapsed = now - then;       // Date subtraction: interval in milliseconds later.getFullYear()             // => 2010later.getMonth()                // => 0: zero-based monthslater.getDate()                 // => 1: one-based dayslater.getDay()                  // => 5: day of week. 0 is Sunday 5 is Friday.later.getHours()                // => 17: 5pm, local timelater.getUTCHours()             // hours in UTC time; depends on timezone

模式匹配

布尔值

  • 下列这些值会被转换成false
    undefined
    null
    0
    -0
    NaN
    “” // the empty string
  • 所有其他值,包括所有对象(数组)都会转换成true
  • false和上面

null和undefined

typeof(null)        //object,一个特殊的对象值,‘非对象’typeof(undefined)   //undefined,未定义
  • null,表示数字、字符串和对象是“无值”的
  • undefined,用未定义的值,表示深层次的“空值”,表示没有初始化
  • 要查询对象或数组元素的值时返回undefined时,说明这个属性或元素不存在
null==undefined             //truenull===undefined            //false

全局对象

包装对象

  • 存取字符串、数字和布尔值的属性时创建的临时对象称作包装对象
  • 字符串不是对象,但为什么会有属性?只要引用了字符串s的属性,JavaScript就会将字符串通过调用new String(s)转换成对象,一旦调用结束,这个对象就会销毁

不可变的原始值和可变的对象引用

-JavaScript中的原始值(undefined、null、布尔值、数字和字符串),是不可改变的
-对象是可变的

类型转换

转换和相等性

null == undefined   // These two values are treated as equal."0" == 0            // String converts to a number before comparing.0 == false          // Boolean converts to number before comparing."0" == false        // Both operands convert to numbers before comparing.

显示类型转换

  • Number类定义的toString()可以接收表示转换基数的可选参数,如果不指定,默认转成10进制。
var n = 17;binary_string = n.toString(2);          // Evaluates to "10001"octal_string = "0" + n.toString(8);     // Evaluates to "021"hex_string = "0x" + n.toString(16);     // Evaluates to "0x11
  • parseInt()和parseFloat()函数是全局函数,不属于任何类。
  • parseInt()只解析整数,parseFloat()可以解析整数和浮点数
  • 如果字符前缀是“0x”或者“0X”,parseInt()将其解析为16进制
  • parseInt()和parseFloat()都会跳过任意数量的前导空格,如果第一个就为非数字,则返回NaN
parseInt("3 blind mice")    // => 3parseFloat(" 3.14 meters")  // => 3.14parseInt("-12.34")          // => -12parseInt("0xFF")            // => 255parseInt("-0XFF")           // => -255parseFloat(".1")            // => 0.1parseInt("0.1")             // => 0parseInt(".1")              // => NaN: integers can't start with "."parseFloat("$72.47"); // => NaN: numbers can't start with "$
  • parseInt()可以接收第二个参数,此参数指定转换哪个进制,范围2~36
parseInt("11", 2);      // => 3 (1*2 + 1)parseInt("ff", 16);     // => 255 (15*16 + 15)parseInt("zz", 36);     // => 1295 (35*36 + 35)parseInt("077", 8);     // => 63 (7*8 + 7)parseInt("077", 10);    // => 77 (7*10 + 7)

对象转换 为原始值

  • toString(), 对象转换为字符串
  • valueOf(),如果对象有原始值,则直接转化为原始值,如果没有,则返回对象本身。
  • 日期类的valueOf()方法,则返回1970年1月1日以来的毫秒数
var now = new Date(); // Create a Date objecttypeof (now + 1) // => "string": + converts dates to stringstypeof (now - 1) // => "number": - uses object-to-number conversionnow == now.toString() // => true: implicit and explicit string conversionsnow > (now -1) // => true: > converts a Date to a number

变量声明

变量作用域

原创粉丝点击