您的位置:首页 > Web前端 > JavaScript

js 如何删除对象中的一个属性

2011-10-17 09:00 489 查看




Deleting Properties

The only way to actually remove a property from an object is to use the
delete
operator;
setting the property to
undefined
or
null
only
remove the value associated
with the property, but not the key.

var obj = {
    bar: 1,
    foo: 2,
    baz: 3
};
obj.bar = undefined;
obj.foo = null;
delete obj.baz;

for(var i in obj) {
    if (obj.hasOwnProperty(i)) {
        console.log(i, '' + obj[i]);
    }
}


The above outputs both
bar
 undefined
and
foo
 null
- only
baz
was
removed and is therefore missing from the output.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: