javascript权威指南笔记(第1章~第5章)

第一章 javascript概述 (略)

 


第二章 词法结构(略)

 


第三章 类型,值和变量

1、 全局属性  :undefined, Infinity, and NaN 

  全局函数 :isNaN(), parseInt() , and eval() 

  构造函数 :Date(), RegExp(), String(), Object(), and Array() 

 

  全局对象:Math and JSON  

2、包装对象与临时对象

3、数组复制代码:

var a = ['a', 'b', 'c'];
var b = [];
for (var i = 0; i < a.length; i++) {
    b[i] = a[i];
}

4、判断两个数组相等代码:

function equalArrays(a, b) {
    if (a.length != b.length) return false;
    for (var i = 0; i < a.length; i++)
        if (a[i] !== b[i]) return false;
    return true;
}

5、 JavaScript type conversions (类型转换表)

6、显示转换

Number("3"); // => 3
String(false);  // => "false" Or use false.toString() 
Boolean([]); // => true
Object(3); // => new Number(3)

7、隐式转化

x + "";               // Same as String(x)
+x;                  // Same as Number(x). You may also see x-0
!!x;                // Same as Boolean(x). Note double !

 8、常用的Number方法

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"

var n = 123456.789;
n.toFixed(0);// "123457"
n.toFixed(2); // "123456.79"
n.toFixed(5); // "123456.78900"
n.toExponential(1); // "1.2e+5"
n.toExponential(3); // "1.235e+5"    有效数字比指定的位数多一位
n.toPrecision(4); // "1.235e+5"      有效数字的位数是否少于数字整数部分的位数
n.toPrecision(7); // "123456.8"
n.toPrecision(10);// "123456.7890"


parseInt("3 blind mice")// => 3
parseFloat(" 3.14 meters") // => 3.14
parseInt("-12.34") // => -12
parseInt("0xFF") // => 255
parseInt("0xff") // => 255
parseInt("-0XFF") // => -255
parseFloat(".1") // => 0.1
parseInt("0.1")// => 0
parseInt(".1")// => NaN: integers can't start with "."
parseFloat("$72.47");// => NaN: numbers can't start with "$"

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)

 9、toString()与valueOf()

({x:1, y:2}).toString() // => "[object Object]"

 10、嵌套作用域

var scope = "global scope";
function checkscope() {    // A global variable
    var scope = "local scope";

    function nested() {    // A local variable
        var scope = "nested scope"; // A nested scope of local variables
        return scope; // Return the value in scope here
    }
    return nested();
}

checkscope();// => "nested scope"

11、变量提前声明,so应该尽量把声明变量放在函数体顶部…………

12 、作用链域、对象列表、变量解析(important)更详细的见博客专题部分


第四章 表达式和运算符 

1、调用表达式

2、对象创建表达式

//note 这里的括号可以省略
new Object();  // => new Object ;
new Date();  // => new Date ;
new Point(2,3);

3、delete一个属性就像(但不完全一样)给这个属性赋值undefined

4、“+”运算符

1 +2                // => 3: addition
"1" + "2"        // => "12": concatenation
"1" + 2         // => "12": concatenation after number-to-string
1 + {}          // => "1[object Object]": concatenation after object-to-string
true + true     // => 2: addition after boolean-to-number
2 + null            // => 2: addition after null converts to 0
2 + undefined       // => NaN: addition after undefined converts to NaN

 5、逻辑”与或非“的使用技巧:

if (a == b) stop(); // Invoke stop() only if a == b
(a == b) && stop(); // This does the same thing

var max = max_width || preferences.max_width || 500;

6、eval()

  ES3不允许任何的解析器对eval赋予别名,否则会抛出一个EvalError

  局部eval和全局eval:

var globaleval = eval;  //使用个别名调用的eval将是全局的eval
var x = "global", y = "global";
function f() {
    var x = "local";
    eval("x += 'changed';");
    return x;
}
function g() {
    var y = "local";
    geval("y += 'changed';");
    return y;
}
console.log(f(), x); // Local variable changed: prints "localchanged global":
console.log(g(), y); // Global variable changed: prints "local globalchanged":

  ie中的execScript()函数:类似于eval,不同的是它总是返回null

7、delete运算符

  delete一个数组的元素,会使数组留下一个“洞”,即只会删除该元素而不会修改该数组的长度

  内置核心和客户端属性是不能删除的,用户通过var声明的变量是不能删除的,function语句定义的函数和函数参数也是不能删除的……

  delete操作后会返回true或false……

8、void运算符: 操作数会照常计算,但是忽略计算结果并返回undefined,常见的例子如下:

<a href="javascript:void window.open();">Open New Window</a>

9、逗号表达式:

  i=0 , j=1 , k = 3;    //首先计算左操作数,然后计算右操作数,最后返回右操作数,注意总会计算左侧表达式,但是会忽略计算结果


第五章 表达式和运算符

1、表达式语句与声明语句

2、 复合语句与空语句

  符合语句:

  {

    语句1;

    语句2;

  }

空语句:如何使用空语句时最好在后面加注释

for(i = 0; i < a.length; a[i++] = 0) /* empty */ ;

 

//函数定义表达式
var fn = function(x) {return x * x;};
//函数声明语句
function fn(x) {return x * x;} 

  函数申明语句通常出现在javascript代码的最顶层,也可以嵌套在其他函数体内,但是在嵌套时,函数声明只能出现在所嵌套函数的顶部,即函数定义不能出现在if,while或其他语句中;

  相同点:都包含相同的函数名,创建了新的函数变量

  不同点:函数声明语句函数名是一个变量,变量指向函数对象,与使用var语句一样,创建的变量不可删除且会被提前到脚本或函数的顶部

      使用var的话,只有变量声明提前了,变量的初始化代码仍然在原来的位置,而使用函数声明语句的话,函数名称和函数名都提前

3、在switch语句中,case 只指明了要执行代码的起点,但是并没有指明终点,所以要用return或是break来终止switch语句

4、将所有对象的属性复制到一个数组中:

var o = {x:1, y:2, z:3};
var a = [], i = 0;
for(a[i++] in o) /* empty */;

5、throw语句

function factorial(x) {
// If the input argument is invalid, throw an exception!
if (x < 0) throw new Error("x must not be negative");
// Otherwise, compute a value and return normally
for(var f = 1; x > 1; f *= x, x--) /* empty */ ;
return f;
}

 Error类型的name属性和mesage属性,

抛出异常时,javascript解析器会立即停止正在执行的逻辑,并跳转到最近的异常处理程序的try从句。=》会一直传播如果没有找到异常处理程序,则会把异常当做程序错误来处理,并报告给用户

6、try/catch/finally语句:

尽管catch语句和finally都是可选的,但是try从句需要至少两者之一来最完整的句子,如果不存在处理异常的catch语句中,则解释器会首先执行

finally中的逻辑,然后向上传播,知道能找到处理这个异常的catch语句、

try {
 //通常,这里的代码会从头执行到尾而不产生任何问题,但有时会抛出一个异常,或者由throw抛出或者调用一个方法间接抛出
 //总之,这里要么不产生任何问题,要么抛出异常
 //终止try语句块的方式有:
 //1、正常终止,执行完语句快的最后一句
 //2、通过break,continue,return语句终止
 //3、抛出一个异常被catch从句捕获
 //4、未被catch从句捕获,继续向上传播
}
catch (e) {
//当且仅当try抛出异常才会执行这里,参数e代表Error对象,或者抛出其他任何值
//注意:这里的catch子句的标示符具有块级作用域 //这里的代码可以处理这个异常,也可以忽略这个异常 //还可以通过throw语句重新抛出异常 } finally{ //不管try语句是否抛出了异常,这里的代码总会执行,
//通常用于处理try从句的清理工作,
//一般,解释器执行到try语句的尾部然后执行finally中的逻辑,即使使用return,continue,break来跳出try语句时,解释器还是会执行finally中的逻辑
}

 使用try/finally来模拟continue的for循环:

// Simulate for( initialize ; test ; increment ) body;
initialize ;
while( test ) {
try { body ; }
finally { increment ; }
}

7、debugger语句:

function f(o) {
if (o === undefined) debugger; // Temporary line for debugging purposes
... // The rest of the function goes here.
}

 分析:当调用f()的时候没有传入参数,程序将停止执行,这是可以通过调试器检测对用栈并找出错误产生的原因,但是debugger语句本身不会

启动调试器

8、use strict

可以用以下代码检测时候支持严格模式:

var hasStrictMode = (function() { "use strict"; return this===undefined}());

 关于严格模式和非严格模式的主要区别:主要的差别见书本

 

原文地址:https://www.cnblogs.com/liguwe/p/3951008.html