这篇文章主要为大家详细总结了 JavaScript 面试开发常用的知识点,感兴趣的小伙伴们可以参考一下
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
No1. 语法和类型
1. 声明定义
变量类型:var,定义变量;let,定义块域 (scope) 本地变量;const,定义只读常量。
变量格式:以字母、下划线 "_" 或者 $ 符号开头,大小写敏感。
变量赋值:声明但未赋值的变量在使用时值为 undefined,未声明的变量直接使用会抛异常。
未赋值变量作计算:结果为 NaN。例如:
var x, y = 1;
console.log(x + y); // 结果为 NaN,因为 x 没有赋值。
2. 作用域
变量作用域:在 ES6 之前没有块声明域,变量作用于函数块或者全局。如下面的代码输入的 x 为 5。
- if (true) {
- var x = 5;
- }
- console.log(x); // 5
ES6 变量作用域:ES6 支持块作用域,但需要使用 let 声明变量。下面的代码输出结果抛出异常。
- i f(true) {
- let y = 5;
- }
- console.log(y); // ReferenceError: y is not defined1234
变量上浮:在一个方法或者全局代码中,我们在生命变量之前使用变量时并没有抛异常,而是返回 undefined。这是因为 javascript 自动把变量的声明上浮到函数或者全局的最前面。如下面的代码:
- /**
- * 全局变量上浮
- */
- console.log(x === undefined); // logs "true"
- var x = 3;
- /**
- * 方法变量上浮
- */
- var myvar = "my value";
- // 打印变量myvar结果为:undefined
- (function() {
- console.log(myvar); // undefined
- var myvar = "local value";
- })();
- 上面代码和下面代码是等价的:
- /**
- * 全局变量上浮
- */
- var x;
- console.log(x === undefined); // logs "true"
- x = 3;
- /**
- * 方法变量上浮
- */
- var myvar = "my value";
- (function() {
- var myvar;
- console.log(myvar); // undefined
- myvar = "local value";
- })();
全局变量:在页面中,全局对象是 window,所以我们访问全局变量可通过 window.variable。例如:
- version = "1.0.0";
- console.log(window.version); //输出1.0.0
No2. 数据结构和类型
1. 数据类型
6 个基础类型:Boolean(true 或者 false)、null(js 大小写敏感,和 Null、NULL 是有区别的)、undefined、Number、String、Symbol(标记唯一和不可变)
一个对象类型:object。
object 和 function:对象作为值的容器,函数作为应用程序的过程。
2. 数据转换
函数:字符串转换为数字可使用 parseInt 和 parseFloat 方法。
parseInt:函数签名为 parseInt(string, radix),radix 是 2-36 的数字表示数字基数,例如十进制或者十六进制。返回结果为 integer 或者 NaN,例如下面输出结果都为 15。
- parseInt("0xF", 16);
- parseInt("F", 16);
- parseInt("17", 8);
- parseInt(021, 8);
- parseInt("015", 10);
- parseInt(15.99, 10);
- arseInt("15,123", 10);
- parseInt("FXX123", 16);
- parseInt("1111", 2);
- parseInt("15*3", 10);
- parseInt("15e2", 10);
- parseInt("15px", 10);
parseFloat:函数签名为 parseFloat(string),返回结果为数字或者 NaN。例如:
- parseFloat("3.14"); //返回数字
- parseFloat("314e-2"); //返回数字
- parseFloat("more non-digit characters"); //返回NaN
3. 数据类型文本化
文本化类型:Array、Boolean、Floating-point 、integers、Object、RegExp、String。
Array 中额外的逗号情况:["Lion", , "Angel"],长度为 3,[1] 的值为 undefiend。['home', , 'school',],最后一个逗号省略所以长度为 3。[,'home', ,'school'],长度为 4。['home', ,'school', ,],长度为 4。
integer 整数:整数可以表达为十进制、八进制、十六进制、二进制。例如:
- 0,
- 117 and - 345 //十进制
- 015,
- 0001 and - 0o77 //八进制
- 0x1123,
- 0x00111 and - 0xF1A7 //十六进制
- 0b11,
- 0b0011 and - 0b11 1234 //二进制
- 浮点数: [( + |-)][digits][.digits][(E | e)[( + |-)] digits]。例如:3.1415926, - .123456789, - 3.1E + 12(3100000000000),.1e-23(1e-24)
对象:对象的属性获取值可通过 ". 属性" 或者 "[属性名]" 获取。例如:
- var car = {
- manyCars: {
- a: "Saab",
- "b": "Jeep"
- },
- 7 : "Mazda"
- };
- console.log(car.manyCars.b); // Jeep
- console.log(car[7]); // Mazda
对象属性:属性名可以是任意字符串或者空字符串,无效的名字可通过引号包含起来。复杂的名字不能通过. 获取,但可以通过 [] 获取。例如:
- var unusualPropertyNames = {
- "": "An empty string",
- "!": "Bang!"
- }
- console.log(unusualPropertyNames.""); // SyntaxError: Unexpected string
- console.log(unusualPropertyNames[""]); // An empty string
- console.log(unusualPropertyNames.!); // SyntaxError: Unexpected token !
- console.log(unusualPropertyNames["!"]); // Bang!
转意字符:下面的字符串输出结果包含了双引号,因为使用了转意符号 "\""。
- var quote = "He read \"The Cremation of Sam McGee\" by R.W. Service.";
- console.log(quote);
- //输出:He read "The Cremation of Sam McGee" by R.W. Service.1。
字符串换行法:直接在字符串行结束时添加 "\",如下代码所示:
- var str = "this string \
- is broken \
- across multiple\
- lines."
- console.log(str); // this string is broken across multiplelines.
No3. 控制流和错误处理
1. 块表达式
作用:块表达式一般用于控制流,像 if、for、while。下面的代码中 {x++;} 就是一个块声明。
- while (x < 10) {
- x++;
- }
ES6 之前没有块域范围:在 ES6 之前,在 block 中定义的变量实际是包含在方法或者全局中,变量的影响超出了块作用域的范围。例如下面的代码最终执行结果为 2,因为 block 中声明的变量作用于方法。
- var x = 1;
- {
- var x = 2;
- }
- console.log(x); // outputs 2
ES6 之后有块域范围:在 ES6 中,我们可以把块域声明 var 改成 let,让变量只作用域 block 范围。
2. 逻辑判断
判断为 false 的特殊值:false、undefined、null、0、NaN、""。
简单 boolean 和对象 Boolean 类型:简单 boolean 类型的 false 和 true 与对象 Boolean 类型的 false 和 true 是有区别,两者是不相等的。如下面的例子:
- var b = new Boolean(false);
- if (b) // 返回true
- if (b == true) // 返回false
No4. 异常处理
1. 异常类型
抛出异常语法:抛异常可以是任意类型。如下所示。
- throw "Error2"; // 字符串类型
- throw 42; // 数字类型
- throw true; // 布尔类型
- throw {
- toString: function() {
- return "I'm an object!";
- }
- }; //对象类型
自定义异常:
- // 创建一个对象类型UserException
- function UserException(message) {
- this.message = message;
- this.name = "UserException";
- }
- //重写toString方法,在抛出异常时能直接获取有用信息
- UserException.prototype.toString = function() {
- return this.name + ': "' + this.message + '"';
- }
- // 创建一个对象实体并抛出它
- throw new UserException("Value too high");
2. 语法
关键字:使用 try{}catch(e){}finally{} 语法,和 C# 语法相似。
finally 返回值:如果 finaly 添加了 return 语句,则不管整个 try.catch 返回什么,返回值都是 finally 的 return。如下所示:
- function f() {
- try {
- console.log(0);
- throw "bogus";
- } catch(e) {
- console.log(1);
- return true; // 返回语句被暂停,直到finally执行完成
- console.log(2); // 不会执行的代码
- } finally {
- console.log(3);
- return false; //覆盖try.catch的返回
- console.log(4); //不会执行的代码
- }
- // "return false" is executed now
- console.log(5); // not reachable
- }
- f(); // 输出 0, 1, 3; 返回 false
finally 吞并异常:如果 finally 有 return 并且 catch 中有 throw 异常。throw 的异常不会被捕获,因为已经被 finally 的 return 覆盖了。如下代码所示:
- function f() {
- try {
- throw "bogus";
- } catch(e) {
- console.log('caught inner "bogus"');
- throw e; // throw语句被暂停,直到finally执行完成
- } finally {
- return false; // 覆盖try.catch中的throw语句
- }
- // 已经执行了"return false"
- }
- try {
- f();
- } catch(e) {
- //这里不会被执行,因为catch中的throw已经被finally中的return语句覆盖了
- console.log('caught outer "bogus"');
- }
- // 输出
- // caught inner "bogus"
系统 Error 对象:我们可以直接使用 Error{name, message} 对象,例如:throw (new Error('The message'));
来源: http://www.phperz.com/article/17/0312/263719.html