javascript
JS 基础 —— JavaScript 关键字(keyword)与保留字
轉(zhuǎn)載于: JavaScript 關(guān)鍵字(keyword)與保留字
一、JavaScript 所有關(guān)鍵字:
break case catch continue default delete do else finally for function if in instanceof new return switch this throw try typeof var void while with
主要想整理下幾個(gè)自己不怎么了解的關(guān)鍵字:
?
1、delete:刪除對(duì)象(而非構(gòu)造方法或原型)的某個(gè)屬性
function obj(id,name){this.id = id;this.name = name;this.getName = function(){return this.name;} } var objOne = new obj(1,"objOneName"); var objTwo = new obj(2,"objTwoName"); alert("objOne名字為:"+objOne.getName());//提示objOneName delete objTwo.name; alert("objOne名字為:"+objOne.getName());//提示objOneName注意:只能刪除自身定義的公有屬性,即"this.屬性名"定義的屬性,而不能刪除私有屬性或通過(guò)proptotype定義的公有屬性(已實(shí)踐)。此外可刪除直接在對(duì)象上添加的屬性,如var a = new Object();a.name = "name";delete a.name;
?
2、throw 與 try...catch...finally:操作異常
JS 的幾種具體異常類(lèi)型(都繼承自Error 異常類(lèi)):
(1)、SyntaxError:語(yǔ)法錯(cuò)誤,如代碼中不小心按了個(gè)特殊符號(hào);
(2)、ReferenceError:引用錯(cuò)誤,常見(jiàn)提示:Uncaught ReferenceError: a is not defined;
(3)、RangeError:范圍錯(cuò)誤,常見(jiàn)對(duì)數(shù)組的操作等;
(4)、TypeError:類(lèi)型錯(cuò)誤,如:xxx is not a function;
(5)、.URLError:與url相關(guān)參數(shù)不正確,主要是encodeURI()、decodeURI()、encodeURIComponent()、decodeURIComponent()、escape()和unescape()這六個(gè)函數(shù);
(6)、EvalError:全局函數(shù)eval 執(zhí)行錯(cuò)誤(注:eval() 函數(shù)可計(jì)算某個(gè)字符串,并執(zhí)行其中的的 JavaScript 代碼)。
throw 可拋出自定義對(duì)象,并可通過(guò)catch捕獲:
<script> function CommonException(message, code) { this.message = message; this.code = code; } try { var exception = new CommonException('您的代碼出錯(cuò)啦', 1); throw exception; alert('這地方的代碼將不會(huì)執(zhí)行到'); } catch (e) { alert(e.message); alert(e.code) } </script>(參考:http://blog.csdn.net/zhang197093/article/details/52055850)
?
3、in:與for 一起使用用于遍歷對(duì)象的屬性名
(1)、使用點(diǎn)一:在js中,for……in用于遍歷一個(gè)對(duì)象的屬性,把對(duì)象的屬性名和屬性值都提出來(lái)
var obj = {"key1":"value1","key2":"value2","key3":"value3" };//屬性名 function EnumaKey(){for(var key in obj ){alert(key);} }//屬性值 function EnumaVal(){for(var key in obj ){alert(obj[key]);} }重點(diǎn):在使用for in遍歷對(duì)象時(shí)候,對(duì)象有一個(gè)重要的方法:hasOwnProperty()。該方法可以在遍歷對(duì)象屬性的時(shí)候可以過(guò)濾掉從原型鏈上下來(lái)的屬性。
?
?
(2)、使用點(diǎn)二:判斷某個(gè)對(duì)象是否具有某個(gè)屬性
對(duì)于一般的對(duì)象屬性需要用字符串指定屬性的名稱(chēng) ??如:
var mycar = {make: "Honda", model: "Accord", year: 1998}; "make" in mycar // returns true "model" in mycar // returns true對(duì)于數(shù)組對(duì)象,元素值對(duì)應(yīng)的屬性名為數(shù)字類(lèi)型,如:
// Arrays var trees = new Array("redwood", "bay", "cedar", "oak", "maple"); 0 in trees // returns true 3 in trees // returns true 6 in trees // returns false "bay" in trees // returns false (you must specify the index number,// not the value at that index) "length" in trees // returns true (length is an Array property)注意:in 的右邊必須是一個(gè)對(duì)象,如:你可以指定一個(gè)用String構(gòu)造器生成的,但是不能指定字符串直接量的形式:
var color1 = new String("green"); "length" in color1 // returns true var color2 = "coral"; "length" in color2 // generates an error (color is not a String object)此外,如果你使用delete操作符刪除了一個(gè)屬性,再次用in檢查時(shí),會(huì)返回false;
如果你把一個(gè)屬性值設(shè)為undefined,但是沒(méi)有使用delete操作符,使用in檢查,會(huì)返回true。
(參考:http://www.cnblogs.com/renxiaoren/p/5942767.html)
4、instanceof :返回的是布爾值,而typeof 返回的是幾種數(shù)據(jù)類(lèi)型的字符串值。
?
5、with:引用一個(gè)對(duì)象,使訪(fǎng)問(wèn)屬性與方法更加方便(只能訪(fǎng)問(wèn)與修改屬性,不能增加屬性與方法)
function obj(id,name){this.id = id;this.name = name;this.getName = function(){return this.name;} } var myObj = new obj(3,"three"); with(myObj){alert(id);//提示3alert(name);//提示threealert(getName());//提示threeid = 4;alert(id);//提示4 } alert(myObj.id);//提示4,說(shuō)明with中是通過(guò)引用方式訪(fǎng)問(wèn)的,而不是復(fù)制值的方式二、JavaScript 所有保留字
保留字是為以后js的擴(kuò)展用的,不能作為普通變量名。列表:
abstract boolean byte char class const debugger double enum export extends final float goto implements import int interface long native package private protected public short static super synchronized throws transient volatile?
總結(jié)
以上是生活随笔為你收集整理的JS 基础 —— JavaScript 关键字(keyword)与保留字的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: nike如何抽签买鞋(耐克Nike)
- 下一篇: gradle idea java ssm