JavaScript所有关键字
breakcasecatchcontinuedefaultdeletedoelsefinallyforfunctionifininstanceofnewreturnswitchthisthrowtrytypeofvarvoidwhile
delete
:删除对象的某个属性
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
in
:与for
一起使用用于遍历对象的属性名
在js中,for……in用于遍历一个对象的属性,把对象的属性名和属性值都提出来
for(var key in obj ){ alert(key); }
判断某个对象是否具有某个属性
对于一般的对象属性需要用字符串指定属性的名称 如:var mycar = {make: "Honda", model: "Accord", year: 1998}; "make" in mycar // returns true "model" in mycar // returns true
对于数组对象,元素值对应的属性名为数字类型,如:
// 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)
此外,如果你使用delete操作符删除了一个属性,再次用in检查时,会返回false;
如果你把一个属性值设为undefined,但是没有使用delete操作符,使用in检查,会返回true。
instanceof
: 判断类型
返回的是布尔值,而typeof
返回的是几种数据类型的字符串值
with
:引用一个对象,使访问属性与方法更加方便
只能访问与修改属性,不能增加属性与方法
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);//提示3 alert(name);//提示three alert(getName());//提示three id = 4; alert(id);//提示4 } alert(myObj.id);//提示4,说明with中是通过引用方式访问的,而不是复制值的方式