农民工吹嘘 發表於 2020-4-2 18:02:00

JavaScript关键字总结

<p><span style="color: rgba(255, 91, 91, 1); font-size: 16px"><strong>js关键字(一共29个关键字)</strong></span>,保留字(就是预备役关键字,不可以用作变量,函数名,对象名等)</p>
<p><strong>break:</strong><span style="color: rgba(153, 204, 0, 1)">立即结束语句,并跳出语句,进行下个语句执行,当即跳出所在的循环。</span></p>
<p><strong>case:</strong>switch表达式与case值作比较,相等时会执行该case中的代码块。后面跟break,阻塞代码继续向下执行。</p>
<p><strong>catch:</strong> <span style="color: rgba(184, 134, 11, 1)">语句允许你处理错误。</span></p>
<p><strong>continue:</strong><span style="color: rgba(153, 204, 0, 1)"> 停止当前语句,并从头执行该语句,直接开始下一循环。</span></p>
<p><strong>debugger:</strong>用于停止执行 JavaScript,并调用 (如果可用) 调试函数。debugger 类似于在代码中设置断点。</p>
<p><strong>default:</strong>有两种使用情况,在 switch中,default只有在case匹配失败的时候才会执行 ,在export 中,导出单个值或需要模块的回掉值可以使用默认导出。</p>
<p><strong>delete:</strong>用于删除对象的某个属性;如果没有指向这个属性的引用,那它最终会被释放。delete object.property,delete object['property']</p>
<p><strong>do:</strong><span style="color: rgba(255, 102, 0, 1)">do/while - 同样当指定的条件为 true 时循环指定的代码块,但该循环在判断条件前会执行一次代码块。</span></p>
<p><strong>else:</strong><span style="color: rgba(0, 204, 255, 1)">使用 else 来规定要执行的代码块,如果相同的条件为 false</span></p>
<p><strong>false:</strong>布尔值</p>
<p><strong>finally:</strong><span style="color: rgba(184, 134, 11, 1)">使你能够执行代码,在 try 和 catch 之后,无论结果如何。</span></p>
<p><strong>for:</strong><span style="color: rgba(183, 183, 0, 1)">1.常规的for(var i=0;i&lt;length;i++)</span></p>
<p><span style="color: rgba(183, 183, 0, 1)">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.for-in:for(var item in list)</span></p>
<p><strong>function:</strong> 定义函数</p>
<p><strong>if:</strong><span style="color: rgba(0, 204, 255, 1)">使用 if 来规定要执行的代码块,如果指定条件为 true</span></p>
<p><strong>in:</strong> <span style="color: rgba(183, 183, 0, 1)">for……in用于遍历一个对象的属性,把对象的属性名和属性值都提出来</span></p>
<p><strong>instanceof:</strong>instanceof 运算符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性。语法:object instanceof constructor</p>
<p><strong>new:</strong>对象创建表达式。new 运算符是用来实例化一个类,从而在内存中分配一个实例对象。可以产生原对象的一个实例对象,而这个实例对象继承了原对象的属性和方法。因此,new存在的意义在于它实现了javascript中的继承,而不仅仅是实例化了一个对象!</p>
<p><strong>null:</strong>表示一个特殊值,常用来描述“空值”。对null执行typeof预算,结果返回字符串“object”,也就是说,可以将null认为是一个特殊的对象值,含义是“非对象”。但实际上,通常认为null是它自有类型的唯一一个成员,它可以表示数字、字符串和对象是“无值”的。</p>
<p><strong>return:</strong>是函数返回语句,但是返回的同时也将函数停止。</p>
<p><strong>switch:</strong>switch表达式与case值作比较,相等时会执行该case中的代码块。后面跟break,阻塞代码继续向下执行。&nbsp;</p>
<p><strong>this:</strong>1.普通函数中的this window</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2.构造函数中的this 是当前构造函数创建的对象在new这个构造函数的时候会在内存中创建一个对象,此时会让 this指向刚创建好的这个对象</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3.方法中的this 方法所属的对象,谁调用这个方法this 就指向谁</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;4.事件处理函数中的this 事件源 谁调用的该事件this就指向谁在这里就指向事件源this</p>
<p><strong>throw:</strong><span style="color: rgba(184, 134, 11, 1)">语句允许你创建自定义错误。(抛出错误)</span></p>
<p><strong>true:</strong>布尔值</p>
<p><strong>try:</strong><span style="color: rgba(184, 134, 11, 1)">语句使你能够测试代码块中的错误。</span></p>
<p><strong>typeof:</strong>typeof 是一个一元运算,放在一个运算数之前,运算数可以是任意类型。它返回值是一个字符串,该字符串说明运算数的类型。typeof 运算符返回一个用来表示表达式的数据类型的字符串。typeof其实就是判断参数是什么类型的实例,就一个参数。typeof 一般只能返回如下几个结果:"number"、"string"、"boolean"、"object"、"function" 和 "undefined"</p>
<p><strong>var:</strong>声明一个变量。let语句在块作用域中声明一个局部变量,let语句允许我们创建一个变量,其范围仅限于使用它的块里。ES6 明确规定,如果区块中存在let和const命令,这个区块对这些命令声明的变量,从一开始就形成了封闭作用域。凡是在声明之前就使用这些变量,就会报错。总之,在代码块内,使用let命令声明变量之前,该变量都是不可用的。这在语法上,称为“暂时性死区”(temporal dead zone,简称 TDZ)。ES6的let让js真正拥有了块级作用域,也是向这更安全更规范的路走,虽然加了很多约束,但是都是为了让我们更安全的使用和写代码。</p>
<p><strong>void:</strong>javascript:void(0)指的是该操作符指定要计算一个表达式但是不返回值。</p>
<p><strong>while:</strong><span style="color: rgba(255, 102, 0, 1)">while - 当指定的条件为 true 时循环指定的代码块,do/while - 同样当指定的条件为 true 时循环指定的代码块,但该循环在判断条件前会执行一次代码块。</span></p>
<p><strong>with:</strong>引用一个对象,使访问属性与方法更加方便(只能访问与修改属性,不能增加属性与方法)。&nbsp;</p>
<p>***********************************************************************************************************************************************</p>
<p><span style="color: rgba(138, 43, 226, 1)"><strong>在 break,continue和return 三个关键字中</strong></span></p>
<p>break,continue是化为一类的,return 是函数返回语句,但是返回的同时也将函数停止。</p>
<p><strong>相同之处:</strong>三个都会将此时进行的语句停止。</p>
<p><strong>不同之处:</strong>1、break:是立即结束语句,并跳出语句,进行下个语句执行。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2、continue:是停止当前语句,并从头执行该语句。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3、return:停止函数。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4、使用的语句环境不一样,break和continue是用在循环或switch语句中,return是用在函数语句中。</p>
<p>&nbsp;</p>
<p><span style="color: rgba(138, 43, 226, 1)"><strong>try和catch的用法</strong></span></p>
<p>try catch 错误处理;</p>
<p>执行规则:首先执行try中的代码 如果抛出异常会由catch去捕获并执行 如果没有发生异常 catch去捕获会被忽略掉 但是不管有没有异常最后都会执行。</p>
<p>try 语句使你能够测试代码块中的错误。</p>
<p>catch 语句允许你处理错误。</p>
<p>throw 语句允许你创建自定义错误。(抛出错误)</p>
<p>finally 使你能够执行代码,在 try 和 catch 之后,无论结果如何。</p>
<p>try 和catch一般是在预计某段代码可能会出错(比如浏览器兼容问题)或者想故意利用这种错误来达成目的(这其实是一种偷懒的行为)时,就可以把这段代码放入try内,然后当出现错误时就会自动去执行catch里的代码。</p>
<p>&nbsp;</p>
<p><span style="color: rgba(138, 43, 226, 1)"><strong>switch和case用法</strong></span></p>
<p>请使用 switch 语句来选择多个需被执行的代码块之一。</p>
<p>语法</p>
<p>switch(表达式) {<br>&nbsp; &nbsp; &nbsp; &nbsp;case n:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;代码块<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br>&nbsp; &nbsp; &nbsp; &nbsp;case n:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;代码块<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;<br>&nbsp; &nbsp; &nbsp; default:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 默认代码块<br>} </p>
<p>代码解释:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;计算一次 switch 表达式</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;把表达式的值与每个 case 的值进行对比</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;如果存在匹配,则执行关联代码</p>
<p>do/while 循环是 while 循环的变体。该循环会执行一次代码块,在检查条件是否为真之前,然后如果条件为真的话,就会重复这个循环。</p>
<p>JavaScript 支持不同类型的循环:</p>
<ul>
<li>for - 循环可以将代码块执行指定的次数。</li>
<li>for/in - 循环遍历对象的属性</li>
<li>while - 当指定的条件为 true 时循环指定的代码块</li>
<li>do/while - 同样当指定的条件为 true 时循环指定的代码块,但该循环在判断条件前会执行一次代码块</li>









</ul>
<p>&nbsp;</p>
<p><span style="color: rgba(138, 43, 226, 1)"><strong>void</strong></span></p>
<p>void 运算符对表达式求值,并返回 undefined。</p>
<p>js中的void后跟一个表达式 void(expression),他会执行这个表达式,然后返回undefined。这里表达式外的括号可以省略。</p>
<p>使用void的主要地方就是a标签,像这样:</p>
<p>&lt;a href = "javascript:void(0);"/&gt;</p>
<p>这样一来,会让点击后不执行任何操作,且不会刷新页面。</p>
<p>这里不这么写也可以,写成这样:</p>
<p>&lt;a href = "javascript:"/&gt;</p>
<p>也是相同的效果,不知道第二种有什么弊端。</p>
<p>也有人在代码中使用void 0来代替undefined,因为undefind不是保留字。所以在某些浏览器下,undefind的值可能会被修改。不过这种情况,学习jquery的方法就可以解决。</p>
<p>(function(p1,p2,undefind){})(p1,p2);</p>
<p>不传第三个参数,那么函数中的undefined就是undefined。</p>
<p>还见过一种在void后写语句的,像这样:</p>
<p>return void expression1,expression2...;</p>
<p>但是函数不指定return的值的话会默认返回undefined。</p>
<p>&nbsp;</p>
<p><span style="color: rgba(51, 204, 204, 1)"><strong>29个关键字</strong></span></p>
<p>break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;typeof       <br>case&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var<br>catch&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; void<br>continue&nbsp; &nbsp; &nbsp; false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; instanceof&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while<br>debugger&nbsp; &nbsp; &nbsp;finally&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with<br>default&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try</p>
<p>&nbsp;</p>
<p><span style="color: rgba(51, 204, 204, 1)"><strong>30个预定义变量和全局函数</strong></span></p>
<p>arguments&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; encodeURI&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Infinity&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Number&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RegExp<br>Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="font-size: 12px">encodeURIComponent</span>&nbsp; &nbsp;isFinite&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String<br>Boolean&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Error&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;isNaN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;parseFloat&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SyntaxError <br>Date&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eval&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSON&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;parseInt&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TypeError   <br>decodeURI&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EvalError&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Math&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RangeError&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;undefined<br><span style="font-size: 12px">decodeURIComponent&nbsp;</span>&nbsp; &nbsp;Function&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NaN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ReferenceError&nbsp; &nbsp; URIError</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/goforxiaobo/p/12621465.html
頁: [1]
查看完整版本: JavaScript关键字总结