JavaScript beginners need to know 10 tips _javascript Tips

Source: Internet
Author: User
Because the vast majority of browsers are compatible with it, you can use it in these browsers. JavaScript is accepted fairly quickly because it is so simple and has a fairly wide range of uses. Many programmers used to think of JavaScript as a "toy language", but Ajax came into the market with the opposite side, allowing JavaScript to exhibit completely different capabilities and capabilities.
As a result of this invention, programmers can now create Web applications with desktop application effects, which is useful because the data can be changed more quickly. Here are some mini tips that can help beginners better use JavaScript. JavaScript is a wide range of uses, and there are so many styles, so it can have a lot of tricks. In addition, although it is a lot of programming methods, but I only selected 10 techniques, I think these skills for beginners to understand JavaScript is a good starting point.
1, add an element at the end of an array
This technique allows you to add an element to the end of an array using the Length property, because the length property is 1 more than the subscript of the last element of the array. This method is the same as the "push" method. For example:
Copy Code code as follows:

var myarray = [];
Myarray[myarray.length] = ' New Element ';

2, adjust the length of an array
The Length property is not read-only, so you can set the value of the length property. Also, you can use it to increase or decrease the length of an array. For example:
Copy Code code as follows:

var myarray = [1,2,3];
Myarray.length//3
Myarray.length = 2; Delete the last element
Myarray.length =//Add elements to the array; The elements have the undefined value.



3, use "!!" Convert any data type to Boolean
This technique allows you to use "!!" Converts any data type (such as String, number, or integer) to a Boolean. For example:
Copy Code code as follows:

var myString = ' 23255 ';
typeof MyString; String
MyString =!! myString;
typeof MyString//boolean

4, convert number to string
This technique allows you to add an empty string at the end of number to convert number to string, for example:
Copy Code code as follows:

var mynumber = 234;
typeof MyNumber; Number
MyNumber + = ';
typeof MyNumber; String

5, understand how many variables a function requires
This is a great technique that allows you to know exactly how many variables a function needs. For example:
Copy Code code as follows:

function Add_nums (NUM1, num2) {
return NUM1 + num2;
}
Add_nums.length//2 is the amount of parameters expected by the function add_nums

6, use the "arguments" object to see how many parameters a function receives
This technique allows you to use the "arguments" object to see how many parameters a function receives. For example:
Copy Code code as follows:

function Add_nums () {
return arguments.length;
}
Add_nums (23,11,32,56,89,89,89,44,6); This is return the number 9

This technique is useful when you need to check the validity of the number of parameters, or when you need to create a function that has an indeterminate number of parameters.
Copy Code code as follows:

function Sum_three_nums () {
if (arguments.length!=3) throw new Error (' received ' + arguments.length + ' parameters and should work with 3 ');
}
Sum_three_nums (23,43); Return to the error message
function Sum_num () {
var total = 0;
For (Var i=0;i<arguments. length;i++) {
Total+=arguments[i];
}
return total;
}
Sum_num (2,34,45,56,56);

7, the object as a parameter to organize and improve the function
In modern web development, one of the most common uses of objects is to treat them as parameters of a function. It's always hard to remember this rule of function arguments, but it's good to use an object because we don't have to worry about the rules of the parameters anymore. Moreover, it is more organized and allows users to better understand what we are going to do. This method allows you to organize and improve functions by taking objects as parameters. For example:
Copy Code code as follows:

function InsertData (name,lastname,phone,address) {
code here;
}

Refactoring the following code is like this:
Copy Code code as follows:

function InsertData (parameters) {
var name = Parameters.name;
var lastName = Parameters.lastname;
var phone = Parameters.phone;
var address = parameters.address;
}

It is also useful when you want to use the default value. For example:
Copy Code code as follows:

function InsertData (parameters) {
var name = Parameters.name;
var lastName = Parameters.lastname;
var phone = Parameters.phone;
var address = parameters.address;
var status = Parameters.status | | ' Single '//if status isn't defined as a property
In the object, variable status take single as value
}

Now, it's very simple to use this function: we can send the data in two different ways.
Copy Code code as follows:

Example 1
InsertData ({name: ' Mike ', lastName: ' Rogers ', Phone: ' 555-555-5555 ', Address: ' The address ', Status: ' Married '});

Example 2
var myData = {name: ' Mike ',
LastName: ' Rogers ',
Phone: ' 555-555-5555 ',
Address: "The Address",
Status: ' Married '
};
InsertData (MyData);

8, the function is the data
Functions are data like strings or numbers, and we can pass them as function arguments, creating very surprising and "commanding" Web applications. This method is very useful, and almost all the mainstream frameworks use this method. For example:
Copy Code code as follows:

function Byid (element, event, F) {
document.getElementById (Element). [' on ' +event] = f; The f is the function, we pass as parameter
}
Byid (' mybtn ', ' click ', function () {alert (' Hello World ')});
Another example of functions as data:
Example 1
function msg (m) {
Alert (m);
}
Example 2
var msg = function (m) {alert (m);}
These functions are almost exactly the same. The only difference is the way they are used. For example: The first function, you can use it before you declare it, but the second function will only be used after the declaration:
Example 1
MSG (' Hello world '); This would work
function msg (m) {
Alert (m);
}
Example 2
MSG (' Hello world '); does not work because JavaScript cannot find this function msg because is used before-is been declared.
var msg = function (m) {alert (M)}

9, extend the local object
Although some JavaScript leaders do not recommend this technique, it has been used by some frameworks. It allows you to create some auxiliary methods for JavaScript APIs.
Copy Code code as follows:

We Create the method prototype for our arrays
It only sums numeric elements
Array.prototype.sum = function () {
var len = this.length;
Total = 0;
for (var I=0;i<len; i++) {
if (typeof this[i]!= ' number ') continue;
Total + = This[i];
}
return total;
}
var myarray = [1,2,3, ' Hola '];
Myarray.sum ();


Array.prototype.max = function () {
Return Math.max.apply (", this");
}

10,boolean
Notice the difference between them because it saves you the time to debug your scripts.
Copy Code code as follows:

' = = ' 0 '//False
0 = = ""/True
0 = ' 0 '/True
False = = ' false '//False
false = = ' 0 '/True
false = = undefined//False
false = = NULL//False
NULL = = undefined//True
true = = 1//True
' = = NULL//False
false = = '/True

If you've seen these scripts elsewhere, these tips can help you get a mastery. These techniques are not even the tip of the iceberg for all the features of JavaScript, but this is a start! Please don't hesitate to leave your comments, questions, extra tips or doubts, but remember, this is an article for beginners!! I hope to receive a letter from some developers! enjoy!

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.