Home > Web Front-end > JS Tutorial > body text

Seven JavaScript skills (1)_javascript skills

WBOY
Release: 2016-05-16 15:25:47
Original
1007 people have browsed it

JavaScript is a very popular programming language. Many developers choose JavaScript as their introductory language. This article shares with you seven practical JavaScript skills, best practices and other very practical content. In the past, if you wanted to create an object, you would do this:

 var car = new Object();
   car.colour = 'red';
   car.wheels = ;
   car.hubcaps = 'spinning';
   car.age = ;  下面的写法能够达到同样的效果: var car = {
   colour:'red',
   wheels:,
   hubcaps:'spinning',
   age:
   }
Copy after login

Much simpler, you don’t need to reuse the name of the object.

In this way, car is defined. Maybe you will encounter the problem of invalidUserInSession. This will only happen when you use IE. Just remember one thing, don’t right brace

Put a semicolon before the

sign and you'll stay out of trouble. ​

Another very convenient abbreviation is for arrays. ​

The traditional way to define an array is as follows:

 var moviesThatNeedBetterWriters
  = new Array(
   'Transformers','Transformers','Avatar','Indiana
  Jones '
   );  简写版的是这样: var moviesThatNeedBetterWriters
  = [
   'Transformers','Transformers','Avatar','Indiana
  Jones '
   ];
Copy after login

For arrays, there is a problem. In fact, there is no graph group function. But you'll often find people defining car above like this

 var car = new Array();
   car['colour'] = 'red';
   car['wheels'] = ;
   car['hubcaps'] = 'spinning';
   car['age'] = ;
Copy after login

Arrays are not omnipotent; this is not written correctly and will confuse people. Graph groups are actually functions of objects, and people confuse the two concepts. ​
Another very cool shorthand method is to use the ternary conditional notation. ​

You don’t have to write like this…

 var direction;
   if(x < ){
   direction = ;
   } else {
   direction = -;
   }
...
Copy after login

You can simplify it using the ternary conditional notation:

var direction

  = x < 200 &#63; 1 : -1; 
Copy after login

When the condition is true, take the value after the question mark, otherwise take the value after the colon. ​

The above is the knowledge about the seven JavaScript skills (1) shared by the editor of Script House. I will update the 7 JavaScript skills (2) later. We will update more in the future. I hope it will be helpful to everyone if I have more knowledge in this area.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template