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: }
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 ' ];
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'] = ;
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 = -; } ...
You can simplify it using the ternary conditional notation:
var direction = x < 200 ? 1 : -1;
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.