- A+
这篇文章将向你分享一些不为人知的但很有用的JavaScript小建议,对那些刚涉及使用JavaScript编程语言的初级开发者应该有很大的帮助。
1. 用数组长度截取数组
我们都知道,对象都是通过使用JavaScript引用的,但这并不是唯一的准则,请看下面的检验案例:
最初,arr1和arr2都指向了数组[1,2,3],之后当arr1重新指向[ ]的时候,arr2的引用并没有发生什么变化,仍然指向[1,2,3]。但是如果我们想让arr1和arr2都指向[ ]的话,那应该怎么做呢?我们可以利用数组的长度属性。当设置arr1.length=0的时候,arr1里面的要素将被清空。而引用是不会改变的,所以arr1和arr2指向[ ]。
2. 用push来合并数组
我们通常使用concat()来合并两个数组,例如:
我们同样可以利用push()来达到这种效果:
应用这种方法可以把一个数组作为第二个参数,因此,arr2能够被推送到arr1里面。
3. 特征检测
在我们使用的很多API里,我们能看到一些特征检测语句,这些语句是用来检查浏览器是否支持特定的属性或方法,以便能够支持跨浏览器兼容性。可以这样做:
这样的运作方式是正确的,但它的缺点是效率不高。这种类型的对象检测将在浏览器里初始化资源。更有效地方式是检查密钥是否在某一个对象里。
4. 检查某一个对象是不是数组
在JavaScript编程语言里,我们可以使用typeof来检查变量的类型,typeof可以返回数字、布尔值、字符串、对象、函数和未定义的对象。事实上这里没有数组,typeof数组就是一个对象。所以我们怎么来决定一个对象就是一个数组呢?在ECMAScript 5编程语言里,我们可以使用 Array.isArray(obj)来检查这里所提到的问题。但是目前ECMAScript 5还没有得到广泛使用。
不过,我们可以使用下面的方法:
2014 年 9 月 29 日 上午 3:03 沙发
I think this is one of the most vital info for me. And i’m glad reading your article.
But want to remark on few general things,
The website style is wonderful, the articles is really excellent : D.
Good job, cheers
2014 年 9 月 30 日 上午 10:38 1层
@Niki Thank you, thank you very much for your appreciation
2014 年 7 月 25 日 下午 2:12 板凳
It is in reality a nice and helpful piece of info.
I’m satisfied that you just shared this useful info with us.
Please stay us up to date like this. Thanks for sharing.
2014 年 7 月 27 日 下午 11:13 1层
@หนังโป๊ Thank you, thank you for your attention!
2014 年 7 月 12 日 上午 11:39 地板
Hi, i think that i noticed you visited my website thus i
came to return the desire?.I am trying to to find issues to
enhance my web site!I suppose its ok to use some of your ideas!!
2014 年 7 月 12 日 上午 11:42 1层
@Franchesca Yeah, you’re welcome to my blog,Can I help you with something?
2014 年 7 月 12 日 上午 11:33 4楼
I absolutely love your blog and find many of your post’s to be what precisely I’m looking for.
Do you offer guest writers to write content for yourself? I
wouldn’t mind creating a post or elaborating on most of the subjects
you write regarding here. Again, awesome site!
2014 年 7 月 12 日 上午 11:38 1层
@Florian Sorry, this is just a personal blog site does not offer someone a writer!
2014 年 7 月 12 日 上午 11:33 5楼
I’m gone to convey my little brother, that he should also pay a
visit this webpage on regular basis to get updated from latest information.
2014 年 7 月 12 日 上午 10:48 6楼
Your style is so unique in comparison to other people I have read stuff from.
I appreciate you for posting when you have the opportunity, Guess I’ll
just book mark this web site.
2014 年 7 月 12 日 上午 11:45 1层
@Sabina Thank you for your support, I will continue!
2014 年 7 月 12 日 上午 10:25 7楼
This design is spectacular! You certainly know how to
keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well,
almost…HaHa!) Excellent job. I really enjoyed what you had to say,
and more than that, how you presented it. Too cool!
2014 年 7 月 10 日 下午 11:02 8楼
Hi, I do think this is a great web site. I stumbledupon it ;
) I may revisit once again since I book-marked it.
Money and freedom is the best way to change, may you be rich and continue to help other people.
2014 年 6 月 28 日 下午 1:36 9楼
Previously, plagiarised content often ranked higher than the original piece.
Happy that the script is now working satisfactorily, I check my emails again – another 25 have arrived since I
last looked, so I spend 45 minutes sorting them out.
The world-wide-web is a fantastic medium from which organizations can attain out to a bigger client base more effortlessly and in a far more successful
manner.
2013 年 9 月 1 日 上午 8:21 10楼
有价值的blog越来越少了,表示支持
癸巳年(蛇)七月廿六 2013-9-1
2013 年 9 月 1 日 下午 1:32 1层
@联信资源网 谢谢支持