js关键字

JavaScript一切关键字

break
case
catch
continue
default
delete
do
else
finally
for
function
if
in
instanceof
new
return
switch
this
throw
try
typeof
var
void
while

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中是经由过程援用体式格局接见的,而不是复制值的体式格局
    原文作者:吴静仪
    原文地址: https://segmentfault.com/a/1190000018982458
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞