The success of JavaScript is a great relish, writing JavaScript code for Web pages is the basic skill of all web designers, and this interesting language contains many things that are not well known, even for years of JavaScript programmers who have not fully understood them. This article is about 7 aspects of JavaScript that you are not familiar with but very practical techniques.
Brief statement
JavaScript can use abbreviated statements to quickly create objects and arrays, such as the following code:
You can use the following shorthand statements:
Object car to create this, but you need to pay special attention to the end of the curly braces must not add ";" otherwise in IE will encounter a lot of trouble.
the traditional way to create arrays is:
Use abbreviated statements:
Another place where you can use a shorthand statement is a conditional judgment statement:
can be abbreviated as:
JSON data format
JSON is the acronym for "JavaScript Object notation", designed by Douglas Crockford, and JSON changes the plight of JavaScript in caching complex data formats, as in the following example, if you want to describe a band, you can write:
You can use JSON directly in JavaScript, or even as a return data object for some APIs, the following code calls an API for the famous bookmark site delicious.com, returns all of your bookmarks on the site, and displays it on your own site:
JavaScript local functions (Math, Array, and String)
JavaScript has many built-in functions that can be used effectively to avoid a lot of unnecessary code, such as finding the maximum value from an array, the traditional method is:
Using built-in functions can be easier to implement:
Another method is to use the Math.max () method:
You can use this method to help probe the browser
This solves a problem with IE, in this way, you can always find the correct value, because the browser does not support that value will return to undefined.
You can also use JavaScript's built-in split () and join () functions to handle the CSS class name of an HTML object, and if the HTML object's class name is a space-separated number of names, you need to pay special attention when appending or deleting a CSS class name, if the object does not have a class name , you can give it directly to the new class name, and if you already have a class name, you must have a space before the new class name, which is done with the traditional JavaScript method:
Using the split and join methods is much more visually elegant:
Event Agent
Rather than designing a bunch of events in an HTML document, it is better to design an event agent directly, for example, if you have some links, the user does not want to open the link after clicking, but executes an event, the HTML code is as follows:
The traditional event processing is traversing each link, plus the individual event handling:
Using the event agent, you can handle it directly without traversing:
Anonymous functions and Module mode
One problem with JavaScript is that any variable, function, or object, unless defined within a function, is global, meaning that other code on the same page can access and rewrite the variable (ECMA's JavaScript 5 has changed this situation-the translator), Using anonymous functions, you can bypass the problem.
For example, you have such a piece of code, obviously, variable name, age, status will become global variables
To avoid this problem, you can use an anonymous function:
If this function is not invoked, it can be more directly:
If you want to access an object or function within it, you can:
This is the so-called Module mode or single example mode (Singleton), the model is Douglas Crockford, and is widely used in Yahoo User Interface Library YUI.
If you want to call the inside method elsewhere and do not want to use the MyApplication object name before the call, you can return the methods in the anonymous function, even by simply returning:
Code configuration
It's hard to change some code when someone uses your JavaScript code, but it's difficult because it's not easy for everyone to read someone else's code, so instead of creating a code configuration object, the other person needs to change some configuration in order to make changes to the code. Here is a JavaScript configuration object detailed article, simply said:
- Create an object called configuration in your code
- Inside, save all the configurations you can change, such as CSS ID and class name, button label text, descriptive text, localized language settings
- Set the object as a global object so that others can access it directly and overwrite it
You should do the work in the last step, here is a reference to the article, the value of 5 things before delivering the code.
Interacting with the background
JavaScript is a front language, you need other languages to interact with the background, and return data, using AJAX, you can let JavaScript directly use the interaction with the background, the complex data processing to the background processing.
JavaScript Framework
It is a total waste of time to write your own code that adapts to the various browsers, and you should choose a JavaScript framework that allows these complex things to be handled by the framework.
More Resources
- Douglas Crockford on JavaScript
JavaScript Depth Video Tutorial
- The Opera WEB Standards Curriculum
JavaScript detailed