JavaScript开发小建议

  • A+
所属分类:Web开发
云服务器118元/年 佣金12%

转载文章请注明出处http://www.admin10000.com/document/2719.html | WEB开发者 /↘阳光下聆听

这篇文章将向你分享一些不为人知的但很有用的JavaScript小建议,对那些刚涉及使用JavaScript编程语言的初级开发者应该有很大的帮助。

Web
1. 用数组长度截取数组
我们都知道,对象都是通过使用JavaScript引用的,但这并不是唯一的准则,请看下面的检验案例:

changdu

最初,arr1和arr2都指向了数组[1,2,3],之后当arr1重新指向[ ]的时候,arr2的引用并没有发生什么变化,仍然指向[1,2,3]。但是如果我们想让arr1和arr2都指向[ ]的话,那应该怎么做呢?我们可以利用数组的长度属性。当设置arr1.length=0的时候,arr1里面的要素将被清空。而引用是不会改变的,所以arr1和arr2指向[ ]。
2. 用push来合并数组
我们通常使用concat()来合并两个数组,例如:

push()

我们同样可以利用push()来达到这种效果:

push

应用这种方法可以把一个数组作为第二个参数,因此,arr2能够被推送到arr1里面。
3. 特征检测
在我们使用的很多API里,我们能看到一些特征检测语句,这些语句是用来检查浏览器是否支持特定的属性或方法,以便能够支持跨浏览器兼容性。可以这样做:

api

这样的运作方式是正确的,但它的缺点是效率不高。这种类型的对象检测将在浏览器里初始化资源。更有效地方式是检查密钥是否在某一个对象里。

api1

4. 检查某一个对象是不是数组
在JavaScript编程语言里,我们可以使用typeof来检查变量的类型,typeof可以返回数字、布尔值、字符串、对象、函数和未定义的对象。事实上这里没有数组,typeof数组就是一个对象。所以我们怎么来决定一个对象就是一个数组呢?在ECMAScript 5编程语言里,我们可以使用 Array.isArray(obj)来检查这里所提到的问题。但是目前ECMAScript 5还没有得到广泛使用。
不过,我们可以使用下面的方法:

end

 

英文原文:Pixels Tech


下载服务恢复
斐讯S7智能体脂秤京东0元购,激活K码再赚40元现金
绅度男士手表防水时尚款男2017新款韩版学生潮商务钢带休闲手表男【淘宝包邮】
【京东】网路通升级版IPC-1600 Plus工程宝 数字网络模拟视频监控综合测试仪
【京东】0元购-斐讯 K3C AC1900双频全千兆智能无线路由器家用wifi信号放大器宽带光纤路由

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论数:16   其中:访客评论  16   博主回复  0

    • avatar Niki 0

      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

        • avatar xylon 2

          @Niki Thank you, thank you very much for your appreciation

        • avatar หนังโป๊ 0

          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.

          • avatar Franchesca 0

            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!!

              • avatar xylon 2

                @Franchesca Yeah, you’re welcome to my blog,Can I help you with something?

              • avatar Florian 0

                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!

                  • avatar xylon 2

                    @Florian Sorry, this is just a personal blog site does not offer someone a writer!

                  • avatar Yong 0

                    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.

                    • avatar Sabina 0

                      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.

                        • avatar xylon 2

                          @Sabina Thank you for your support, I will continue!

                        • avatar Dessie 0

                          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!

                          • avatar buluo.d1xz.net 0

                            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.

                            • avatar Antonetta 0

                              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.

                              • avatar 联信资源网 0

                                有价值的blog越来越少了,表示支持
                                癸巳年(蛇)七月廿六 2013-9-1