ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] [https://wangdoc.com/javascript/](https://wangdoc.com/javascript/) ## typeof ``` typeof 123 // "number" typeof '123' // "string" typeof false // "boolean" typeof undefined typeof window // "object" typeof {} // "object" typeof [] // "object" typeof null // "object" 历史原因 ``` ## 整数 ### null 与 undefined 区别 都可以在判断的时候表示 fase; 只有在转为数字时候 ``` Number(null) // 0 Number(undefined) // NaN ``` ### 判断为声明的js变量 ``` // 错误的写法 if (v) { // ... } // 报错: v is not defined // 正确的写法 if (typeof v === "undefined") { // ... } ``` ### NaN NaN 是js整数类型的特殊值,保表示非数字 ``` 5 - 'x' // NaN Math.sqrt(-1) // NaN 0 / 0 // NaN typeof NaN // 'number' NaN + 32 // NaN ``` #### 判断是否是 NaN ``` isNaN(NaN) // true isNaN(Number('Hello')) // true ``` 利用 NaN 不等于自身来判断最有效 ``` function myIsNaN(value) { return value !== value; } ``` ### 转为整数 ``` parseInt(' 8') // 8 parseInt('8a') // 8 parseInt('12**') // 12 parseInt('12.34') // 12 parseInt('15e2') // 15 parseInt('15px') // 15 parseInt('0x10') // 16 以十六进制转换 parseInt('abc') // NaN parseInt('.3') // NaN ``` #### 转为进制数 ``` parseInt('1000', 10) // 1000 parseInt('1000', 2) // 8 parseInt('1000', 6) // 216 parseInt('1000', 8) // 512 ``` ### 转为浮点数 ``` parseFloat('3.14') // 3.14 parseFloat('314e-2') // 3.14 如果字符串符合科学计数法,则会进行相应的转换。 ``` ## 字符串 ### Unicode 字符集 ``` var s = '\u0061'; //a var s = '\u00A9'; //© ``` js 只支持 UTF-16 支持不完整,两字节的字符,不支持四字节的字符 ### Base64 转码 - `btoa()`:任意值转为 Base64 编码 - `atob()`:Base64 编码转为原来的值 ``` var string = 'Hello World!'; btoa(string) // "SGVsbG8gV29ybGQh" atob('SGVsbG8gV29ybGQh') // "Hello World!" ``` ## 函数 ### 函数表达式添加函数名 ``` var print = function demo(){ console.log(demo.name); //demo }; print(); ``` ### 函数的属性和方法 #### name 值 ``` function f1() {} f1.name // "f1" var f2 = function () {}; f2.name // "f2" var f3 = function myName() {}; f3.name // 'myName' ``` 用途: 当函数通过参数传入时,可以获取函数名 ``` var myFunc = function () {}; function test(f) { console.log(f.name); } test(myFunc) // myFunc ``` #### length ``` function f(a, b) {} f.length // 2 ``` ### arguments 函数参数 对象,非数组 ``` var f = function (one) { console.log(arguments[0]);//1 console.log(arguments[1]);//2 console.log(arguments[2]);//3 } f(1,2,3) ``` ### 闭包 ``` function a(){ var n =12; function b(){ console.log(n); } return b; } var c = a(); c()//12 ``` 闭包的作用: * 可以读取函数内部的变量, * 这些变量始终保持在内存中,即闭包可以使得它诞生环境一直存在 #### 变量始终保存在内存中 ``` function num(stat){ return function () { return stat++; } } var a = num(0); a();//0 a();//1 a();//2 ``` #### 初始化一个值 ``` function apiConnect(apiKey) {   function get(route) {     return fetch(`${route}?key=${apiKey}`);   }   function post(route, params) {     return fetch(route, {       method: 'POST',       body: JSON.stringify(params),         headers: {           'Authorization': `Bearer ${apiKey}`         }       })   }   return { get, post } } const api = apiConnect('my-secret-key'); // No need to include the apiKey anymore api.get('http://www.example.com/get-endpoint'); api.post('http://www.example.com/post-endpoint', { name: 'Joe' }); ``` ## 数组 数组是一种对象 数组可放入任意类型的值 ``` var arr = [ {a: 1}, [1, 2, 3], function() {return true;} ]; arr[0] // Object {a: 1} arr[1] // [1, 2, 3] arr[2] // function (){return true;} ``` ### length - 如果赋值的长度小于原始长度,则会截取到赋值的长度 - 设置`length=0` ,清空数组 ## 错误处理机制 ### 原生错误类型 - SyntaxError 对象 : 解析代码时发生的语法错误 - ReferenceError 对象 : 引用一个不存在的变量时发生的错误。 - RangeError 对象 : 一个值超出有效范围时发生的错误 - TypeError 对象 : 变量或参数不是预期类型时发生的错误 - URIError 对象 : URI 相关函数的参数不正确时抛出的错误,主要涉及`encodeURI()、decodeURI()、encodeURIComponent()、decodeURIComponent()、escape()和unescape()`这六个函数 例子: ``` var err2 = new RangeError('出错了,变量超出有效范围!'); err3.name// "RangeError" err3.message // "出错了,变量类型无效!" ``` ### 自定义错误 [推荐] ``` function UserError(message,code){ this.message = message || "默认错误信息"; this.name = "UserError"; this.code = code || undefined; } UserError.prototype = new UserError(); UserError.prototype.constructor = UserError try { throw new UserError("错误消息", 400) ;// 第二参数可不填 } catch (e) { console.log(e.message); console.log(e.code); } ``` ### Boolean,Number,String 类型转换 ``` Boolean('a');//true Number('123');//123 String(true);//true ``` ## 运算符 ### 按位非运算符“~” #### ~value ``` if(arr.indexOf(ele) > -1){...} //易读 if(~arr.indexOf(ele)){...} //简洁 ``` #### ~~value `~~value`可以代替`parseInt(value)` ``` parseInt(-2.99) //-2 ~~(-2.99) //-2 ``` ### 强制转布尔 `!!`