圆月山庄资源网 Design By www.vgjia.com

代码如下

var obj={
	name: 'zhagnsan',
	age: 19 
}
delete obj.name //true
typeof obj.name //undefined

通过delete操作符, 可以实现对对象属性的删除操作, 返回值是布尔

可以删除其他东西吗

1.变量

var name ='zs' //已声明的变量
delete name //false
console.log(typeof name) //String

age = 19 //未声明的变量
delete age	 //true
typeof age //undefined

this.val = 'fds' //window下的变量
delete this.val	 //true
console.log(typeof this.val) //undefined

已声明的变量windows下的变量可以删除, 未声明的变量不可删除

2.函数

var fn = function(){} //已声明的函数
delete fn	//false
console.log(typeof fn) //function

fn = function(){} //未声明的函数
delete fn	//true
console.log(typeof fn) //undefined

3.数组

var arr = ['1','2','3'] ///已声明的数组
delete arr //false
console.log(typeof arr) //object

arr = ['1','2','3'] //未声明的数组
delete arr //true 
console.log(typeof arr) //undefined

var arr = ['1','2','3'] //已声明的数组
delete arr[1] //true
console.log(arr) //['1','empty','3'] 

4.对象

var person = {
 height: 180,
 long: 180,
 weight: 180,
 hobby: {
  ball: 'good',
  music: 'nice'
 }
}
delete person ///false
console.log(typeof person)  //object

var person = {
 height: 180,
 long: 180,
 weight: 180,
 hobby: {
  ball: 'good',
  music: 'nice'
 }
}
delete person.hobby ///true
console.log(typeof person.hobby) //undefined

已声明的对象不可删除, 对象中的对象属性可以删除

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
JS,删除,对象属性

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com