JavaScript Seven Tips (ii) _JAVASCRIPT skills

Source: Internet
Author: User
Tags mathematical functions

The last article introduced JavaScript seven tips (b), writing JavaScript code has been a long time, can not remember what era began. I am very excited about the achievements of JavaScript as a language in recent years, and I am fortunate to be the beneficiaries of these achievements. I have written a lot of articles, chapters, and a book devoted to it, but I can still find some new knowledge about the language. The following description of the past makes me want to send out "Ah!" Exclamation point programming skills that you should try now instead of waiting for a chance to find them sometime in the future.

var band = {
' name ': ' The Red Hot Chili Peppers ',
' members ': [
{
' name ': ' Anthony Kiedis ',
"Role": "Leads vocals"
},
{
"name": "Michael ' Flea ' Balzary",
"role": "Bass guitar, trumpet, back    ing vocals "
},
{
" name ":" Chad Smith ",
" role ":" Drums,percussion "
},
{
' Name ': ' John frusciante ',
' role ': ' Lead Guitar '
}
],
' year ': '
}

You can use JSON directly in JavaScript, and you can encapsulate it in a function, even as a return value form for an API.  We call this json-p, and many APIs use this form. You can call a data supply source and return json-p data directly in the script code:

01
12

This is the list of recent unordered bookmarks in JSON format that calls the Web Service feature provided by the delicious Web site.

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

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

JavaScript's own functions (Math, Array, and String)

One of the things that amazes me is that when I look at the math and string functions in JavaScript, I find that they can greatly simplify my programming work.

Using them, you can dispense with complex loops and conditional judgments.

For example, when I need to implement a function to find the largest number in a numeric 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 do without loops:

 var numbers =

[,,,,];
Numbers.sort (function (a,b) {return B-
A});
Alert (numbers[]);

Note that you cannot sort () a numeric character array, because in this case it will only be sorted alphabetically.
If you want to know more usage, you can read this good article about sort ().

Another interesting function is Math.max ().

This function returns the largest number in the number in the argument:

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

Because this function verifies the number and returns the largest one, you can use it to test the browser's support for an attribute:

 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 depending on the DOCTYPE on the page, only one of these two attributes will hold the value, and the other will be undefined, so you can get this number by using Math.max ().

Read this article and you'll get more knowledge about using mathematical functions to simplify JavaScript.

Another function that has a very useful string of operations is split () and join (). I think the most representative example should be to write a feature that is used to attach CSS styles to page elements.

So, when you attach a CSS class to a page element, either it's the first CSS class for the element, or it has some class, you need to add a space to the existing class, and then append the class. And when you want to remove this class, you also need to remove the space in front of the class (this is important in the past, because some older browsers do not know the class with the following spaces).

So, the original wording would be this:

 function AddClass (elm,newclass) {
var c =
Elm.classname;
Elm.classname = (c = = "")?   Newclass:c+ ' +newclass;
You can perform this task automatically using the split () and join () functions: 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 that the class you want to append is exactly the last.

It's a short-sighted act. Toolkits can help you develop quickly, but if you don't understand JavaScript in depth, you'll do the wrong thing.

Storing data in JSON form

Before I found JSON, I used a variety of crazy methods to store data in JavaScript's intrinsic data types, such as arrays, strings, and the middle with symbols that were easy to split and other annoying things.

After Douglas Crockford invented the JSON, everything changed.

With JSON, you can use JavaScript's own functionality to store data in a complex format without having to do other additional transformations that can be accessed directly.

JSON is the abbreviation for "JavaScript Object notation", which uses the two shorthand methods mentioned above.

The above content is small to share the JavaScript seven great skills, I hope you like.

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.