Seven major JavaScript skills (2) _ javascript skills

Source: Internet
Author: User
I have been writing JavaScript code for a long time, and I cannot remember the beginning of my time. This article will share with you the seven tips of javascript (2). If you are interested in the knowledge of javascript skills, let's take a look. The last article will introduce you to the seven tips of JavaScript (2 ), I have been writing JavaScript code for a long time, and I can't remember the beginning of my time. I am very excited about the achievements of the JavaScript language in recent years. I am lucky to be one of those achievements. I have written many articles, chapters, and a book devoted to it. However, I can still find some new knowledge about this language. The following describes the past, which makes it impossible for me to say "Ah !" You should try these skills now, instead of waiting for the future to discover them by chance.

var band = {   "name":"The Red Hot Chili Peppers",   "members":[   {   "name":"Anthony Kiedis",   "role":"lead vocals"   },   {   "name":"Michael 'Flea' Balzary",   "role":"bass guitar, trumpet, backing vocals"   },   {   "name":"Chad Smith",   "role":"drums,percussion"   },   {   "name":"John Frusciante",   "role":"Lead Guitar"   }   ],   "year":""   }

You can directly use JSON in JavaScript, encapsulate it in a function, or even use it as the return value form of an API. We call this JSON-P, and many APIs use this form. You can call a data provider source to directly return JSON-P data in script code:

01
12

This is to call the Web service function provided by the Delicious website to obtain the list of recent unordered bookmarks in JSON format.

Basically, JSON is the easiest way to describe complex data structures, and it can run in a browser.

You can even use the json_decode () function in PHP to run it.

Built-in functions of JavaScript (Math, Array, and String)

One thing that surprised me was that when I studied math and String functions in JavaScript, I found they greatly simplified my programming work.

With them, you can save complex loop processing and condition judgment.

For example, when I need to implement a function to find the largest number in the number array, I used to write this loop like this:

 var numbers =  [,,,,];   var max = ;   for(var i=;i   if(numbers[i]  > max){   max = numbers[i];   }   }   alert(max);

We can achieve this without loops:

 var numbers =  [,,,,];   numbers.sort(function(a,b){return b -  a});   alert(numbers[]);

Note that you cannot perform sort () on an array of numeric characters, because in this case it will only be sorted alphabetically.
If you want to know more usage, read this good sort () Article.

Another interesting function is Math. max ().

This function returns the largest number in the parameter:

Math.max(12,123,3,2,433,4); // returns 433 

Because this function can verify the number and return the largest one, you can use it to test the browser's support for a feature:

 var scrollTop=  Math.max(   doc.documentElement.scrollTop,   doc.body.scrollTop   );

This is used to solve the IE problem. You can get the scrollTop value of the current page, but according to the DOCTYPE on the page, only one of the above two attributes will store this value, and the other attribute will be undefined, so you can use Math. max () gets this number.

Read this article to learn more about how to use mathematical functions to simplify JavaScript.

In addition, a pair of very useful functions for operating strings are split () and join (). I think the most representative example is to write a function to add CSS styles to page elements.

Yes. When you attach a CSS class to a page element, either it is the first CSS class of the element, or it already has some classes, add a space after the existing class and then append the class. When you want to remove this class, you also need to remove the spaces in front of this class (this is very important in the past, because some old browsers do not know the class followed by spaces ).

The original writing method is as follows:

Function addclass (elm, newclass) {var c = elm. className; elm. className = (c = '')? Newclass: c + ''+ newclass;} You can use the split () and join () functions to automatically complete this task: function addclass (elm, newclass) {var classes = elm. className. split (''); classes. push (newclass); elm. className = classes. join ('');}

This ensures that all classes are separated by spaces, and the class you want to append is placed at the end.

Is short-sighted. The Toolkit can help you develop quickly, but if you do not have a deep understanding of JavaScript, you will also do something wrong.

Store data in JSON format

Before I found JSON, I used various crazy methods to store data in JavaScript's inherent data types, such as arrays, strings, there are symbols that are easy to split and other annoying things in the middle.

After Douglas Crockford invented JSON, everything changed.

Using JSON, you can use JavaScript's own functions to store data in complex formats, and you can directly access and use it without additional conversions.

JSON is the abbreviation of "JavaScript Object Notation". It uses the two abbreviated methods mentioned above.

The above content is the seven tips on javascript that I want to share with you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.