编程小贴士

给你的编程提供小点子


Javascript中的delete操作符

变量

说到javascript中的delete操作符,还是首先要搞清楚javascript中的变量和属性之间的关系。

javascript 中,变量和对象属性关系非常微妙,甚至可以很多时候会被等同起来,因为 javascript 在执行脚本之前会创建一个Global对象,在浏览器中就是window对象,所有的全局变量都是这个Global对象的属性,执行函数时也会创建一个 Activation对象,所有的局部变量都是这个Activation对象的属性。这些可以大家可以去了解一下javascript作用域和闭包。

1 var global = 1;
2 this.global; // 1, 可以通过this来访问Global对象
3 this.global2 = 2;
4 global2; // 12
5 function foo() {
6 var local = 36;
7 // 不过无法直接访问Activation,
8 // 因此无法通过 foo.local 的方式来访问local变量
9 }

似乎变量就等同于对象属性,其实不然,至少对于delete操作符来说还是不一样的。我的理解是变量声明是必须通过 var 语句来完成的,未通过var 语句声明的全局变量都是都是window对象的属性。这样就很容易的理解变量和对象属性的关系了。

delete操作符

delete操作符是用来删除对象属性的。对于引用类型的值,它也是删除对象属性的本身,不会删除属性指向的对象。如果有疑问可以看看基本类型和引用类型的值,或者测试下面的代码:

1 var o = {};
2 var a = { x: 10 };
3 o.a = a;
4 delete o.a; // o.a属性被删除
5 console.log(o.a); // undefined
6 console.log(a.x); // 10, 因为{ x: 10 } 对象依然被 a 引用,所以不会被回收

变量是不能删除的

通过var声明的变量和通过function声明的函数拥有DontDelete特性,是不能被删除。未通过var声明的全局变量(全局对象的属性)

01 var global = {
02 a: 123,
03 b: {
04 c: 1345
05 }
06 };
07 delete global; //无效
08 console.log(global)
09
10 obj = {
11 a: 123
12 };
13 delete obj; // 删除obj全局变量,window对象的obj属性
14 console.log(obj);//obj is not defined

原型中声明的属性和对象自带的属性无法被删除

原型prototype中声明的属性和对象自带的属性(其实这些属性也是在原型prototype中的)可以认为是带有DontDelete的特性的,无法被删除。例如

 

 

//原型中声明的属性无法被删除

 

function Obj() { this.x = 1; }

Obj.prototype.x = 2;

 

var o = new Obj();

console.log(o.x); // 1, 构造函数中定义的o.x

 

delete o.x;

console.log(o.x); // 2, prototype中定义的o.x,即使再次执行delete o.x也不会被删除

 

//对象自带的属性无法被删除

 

var strings=”123456″;

console.log(strings.length);//6

delete strings.length;

console.log(strings.length);//依然是6

 

eval语句下的几个例外

eval执行的代码中,通过var声明的变量虽然与正常的var声明变量同属于Global对象,但它们不具有DontDelete特性,能被删除。但是eval的代码中的函数内通过var定义的变量具有DontDelete,不能被删除。

 

eval(“var x = 42;”);

x; // => 42

delete x;

x; // => ReferenceError: x is not defined

eval(“function f() { return 12; }”);

f(); // => 12

delete f;

f(); // => ReferenceError: f is not defined

//eval执行的代码中,通过var声明的变量虽然与正常的var声明变量同属于Global对象,

// 但它们不具有DontDelete特性,能被删除。

eval(“(function () {” +

” var x = 42;” +

” delete x;” +

” return x;” +

“})();”)

// => 42

// eval的代码中的函数内通过var定义的变量具有DontDelete,不能被删除。

delete的返回值

delete是普通运算符,会返回true或false。当被delete的对象的属性存在并且拥有DontDelete时 返回false,否则返回true。 这里的一个特点就是,对象属性不存在时也返回true,所以返回值并非完全等同于删除成功与否。

 

 

function C() { this.x = 42; }

C.prototype.y = 12;

var o = new C();

 

delete o.x; // true

o.x; // undefined

“x” in o; // false

// o.x存在并且没有DontDelete,返回true

 

delete o.y; // true

o.y; // 12

// o自身没有o.y属性,所以返回true

// 从这里也可以看到prototype链的存在,对象自身属性和prototype属性是不同的

 

delete o; // false

// Global.o拥有DontDelete特性所以返回false

 

delete undefinedProperty; // true

// Global没有名为undefinedProperty的属性因此返回true

 

delete 42; // true

// 42不是属性所以返回true。有的实现会抛出异常(违反ECMAScript标准)

 

var x = 24;

delete x++; // true

x; // 25

// 被删除的是x++的返回值(24),不是属性,所以返回true

65 Responses to “ Javascript中的delete操作符 ”

  1. i love pron说道:

    JvnnXL It as the little changes that make the biggest changes. Many thanks for sharing!

  2. that I have really enjoyed browsing your blog posts.

  3. click说道:

    This is very interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your magnificent post. Also, I ave shared your site in my social networks!

  4. w88world.com说道:

    Im grateful for the post.Much thanks again. Will read on

  5. Wow, marvelous blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is excellent, let alone the content!

  6. very few web-sites that transpire to be comprehensive below, from our point of view are undoubtedly effectively worth checking out

  7. Nwokolo说道:

    I went over this website and I believe you have a lot of fantastic info, bookmarked (:.

  8. klm voucher说道:

    I want forgathering utile information , this post has got me even more info!.

  9. Way cool! Some very valid points! I appreciate you writing this write-up plus the rest of the site is extremely good.

  10. Wow, great article post.Really thank you! Really Cool.

  11. Beyonce说道:

    we like to honor numerous other world-wide-web web-sites around the internet, even when they aren

  12. yeezy boost 350说道:

    Thank you so much for giving everyone such a splendid opportunity to read articles and blog posts from this web site. It is usually so superb plus packed with amusement for me personally and my office friends to visit your website more than three times weekly to find out the newest secrets you have got. Not to mention, I am also at all times happy considering the unique hints you serve. Some 4 ideas in this posting are easily the most effective I have ever had.

  13. sales tool说道:

    This blog is without a doubt interesting as well as diverting. I have found a lot of helpful things out of this source. I ad love to come back again soon. Thanks!

  14. the original materials to represent skin etc. As for the technology of air cushion; it is useful

  15. You have brought up a very wonderful details , appreciate it for the post.

  16. Your blog is one of a kind, i love the way you organize the topics.: a-аАа’аАТ‚аЂТ˜

  17. Very nice info and straight to the point. I am not sure if this is really the best place to ask but do you guys have any ideea where to hire some professional writers? Thanks

  18. money说道:

    I think this is a real great blog post.Thanks Again. Keep writing.

  19. This particular blog is definitely entertaining as well as factual. I have picked helluva helpful tips out of this source. I ad love to visit it again soon. Thanks a bunch!

  20. check this out说道:

    You ave made some really good points there. I checked on the web for more info about the issue and found most people will go along with your views on this web site.

  21. It as not that I want to duplicate your web-site, but I really like the layout. Could you let me know which style are you using? Or was it custom made?

  22. Crochet poncho说道:

    lunette ray ban Is Totally Neat Within A Descendant Typical

  23. Tejido de punto说道:

    Wonderful work! This is the type of information that should be shared around the net. Shame on the search engines for not positioning this post higher! Come on over and visit my web site. Thanks =)

  24. my page说道:

    to mind. Is it simply me or does it look like li?e some of

  25. Usually I don at read post on blogs, however I wish to say that this write-up very pressured me to check out and do it! Your writing style has been amazed me. Thank you, very great post.

  26. School news说道:

    Im grateful for the blog article.Really looking forward to read more. Keep writing.

  27. There is evidently a bundle to identify about this. I think you made various good points in features also.

  28. Usually I don at learn post on blogs, however I would like to say that this write-up very forced me to try and do so! Your writing style has been surprised me. Thanks, very great article.

  29. I simply could not depart your site before suggesting that I really enjoyed the usual information a person provide to your visitors? Is gonna be back frequently to check out new posts

  30. Major thankies for the blog post.Really looking forward to read more. Will read on…

  31. Its hard to find good help I am regularly proclaiming that its hard to find quality help, but here is

  32. Way cool! Some extremely valid points! I appreciate you writing this write-up plus the rest of the website is very good.

  33. Thanks for sharing, this is a fantastic post.Really looking forward to read more. Awesome.

  34. darey说道:

    Very neat blog.Much thanks again. Really Great.

  35. Game Slot Online说道:

    You created some respectable factors there. I seemed on the net for the problem and located many people will go along with together with your internet site.

  36. Simply wanna remark that you have a very nice web site , I love the design and style it really stands out.

  37. Whats Happening i am new to this, I stumbled upon this I have found It absolutely helpful and it has aided me out loads. I hope to give a contribution & help other users like its aided me. Good job.

  38. wow, awesome blog.Really looking forward to read more. Awesome.

  39. niw说道:

    your website and keep checking for new details about once per week.

  40. Lately, I did not give a great deal of consideration to leaving comments on blog web page posts and have positioned remarks even considerably much less.

  41. dclm devotionals说道:

    place at this weblog, I have read all that, so at this time me also commenting here.

  42. check说道:

    Some really nice stuff on this internet site , I love it.

  43. Seriously.. thank you for starting this up. This web site is one thing that as needed on the web, someone with some originality! Here is my weblog useful reference

  44. Wow, incredible weblog structure! How long have you been running a blog for? you made running a blog look easy. The overall look of your web site is wonderful, let alone the content material!

  45. I reckon something genuinely special in this internet site.

  46. Very interesting information!Perfect just what I was searching for! Charity is injurious unless it helps the recipient to become independent of it. by John Davidson Rockefeller, Sr..

  47. Muchos Gracias for your blog post. Keep writing.

  48. It as best to participate in a contest for the most effective blogs on the web. I all recommend this site!

  49. Thanks for the post.Much thanks again. Great.

  50. Major thanks for the blog.Much thanks again. Awesome.

  51. Thank you ever so for you article.Much thanks again. Want more.

  52. Share posts说道:

    Say, you got a nice article.Much thanks again. Really Great.

  53. view说道:

    Looking around While I was surfing yesterday I saw a excellent article about

  54. The account aided me a acceptable deal. I have been a little bit acquainted of this your broadcast offered bright clear

  55. Free ads说道:

    pretty handy stuff, overall I consider this is well worth a bookmark, thanks

  56. Really appreciate you sharing this blog.Thanks Again. Keep writing.

  57. click说道:

    That is a great tip especially to those fresh to the blogosphere. Brief but very accurate info Many thanks for sharing this one. A must read article!

  58. Major thanks for the blog article.Thanks Again. Keep writing.

  59. Really enjoyed this blog article.Much thanks again. Cool.

  60. official site说道:

    It as hard to find educated people in this particular subject, but you sound like you know what you are talking about! Thanks

  61. Very neat article post.Much thanks again. Great.

  62. orlando seo说道:

    Wow, great article post.Much thanks again. Fantastic.

  63. go here说道:

    What web host are you using? Can I get your affiliate link to your host?

  64. Yes, you are correct friend, on a regular basis updating weblog is really essential in favor of SEO. Nice discussion keeps it up.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>