Learn the basics of JavaScript based on knowledge

Source: Internet
Author: User

the difference between JavaScript and Java
1.JavaScript is a product of Netscape, and Java is a sun company product.
2.JavaScript is object-based and Java is object-oriented.
3.JavaScript can be performed only with explanation, Java needs to be compiled into bytecode files before execution.
4.JavaScript is a weak type and Java is strongly typed.
Summary: In fact, Java and JavaScript in addition to the name of some of the image, and then JavaScript borrowed part of the Java idea, the rest of the other little relationship.
Ii. How to combine JavaScript with HTML
1. Label form
We store the JavaScript code in the label on the <script> </script> in. Can be placed in any position.
2. Import method
Use the SRC attribute of the script tag to introduce a JavaScript file.
For example: <script src= "Test.js" type= "JavaScript" ></script>
Note: The type attribute must be added to the script tag in the specification.
three, JavaScript syntax
1. Variable
defined by the keyword Var, because it is a weak type, you do not specify a specific data type.
Example: var x = 3; x = "Hello";
Note: 1 There is no difference between single and double quotes in JavaScript, but it is best to write double quotes, spec.
2 A special constant value in javascript: Undefined, which is used when the variable is not initialized, and the value of the variable is undefined (undefined).
2. Operator
Like other programming languages, it is similar to Java. also supports string connectors (+) and ternary operators (? : The difference is that the ternary operator does not need to have a value and can be directly in the statement output.
3. Statement
The statement format is similar to a variety of programming languages, as well as judgment, selection, and looping. But notice a few things:
1 in JavaScript, Central Africa 0 is true. For example, the following code
var x = 3;
if (x==4)//can perform a comparison operation.
if (x=4)//can be assigned to the operation, and can also be judged. No error. The result is true, so the IF (4==x) can be written backwards after the IF statement, so that if written as 4=x, the error occurs. can correct the error.
2 There is no type restriction inside the switch
3) The cycle must have an end condition
4 Connection Boolean operation, must use && or | |, if use & or |
Four Function
1. General functions
Format:
function functions name (formal parameter ...)
{
Execute the statement;
return value;
}
Note: 1 The return statement can not be written, the function must be called to run.
2 The form parameter is not necessary to add Var, is a weak type.
3 Call a function with parameters, but do not give it a value, or more than the number of parameters, the function can run, or call the function without parameters, pass the value, the function is also running.
4 No overloaded function form in JavaScript
Because you actually encapsulate multiple parameters of a function in a arguments array in JavaScript, you can accept as many arguments as you want, but it's best to pass the actual arguments in a well-defined form parameter.
5) Note the following example
var show = demo ();
The above statement indicates that the show variable receives the return value of the demo function.
var show = demo;
The above statement indicates that show and demo represent the same function, pointing to the consent object
2. Dynamic function
Implemented through the built-in object function of JavaScript. As follows:
var demo = new Function ("X,y", "var sum = x+y; alert (sum);");
Demo (5,2);
The difference between a dynamic function and a general function is that both parameters and function bodies can be passed through parameters and can be specified dynamically.

3. anonymous function
Format: function ()
Example: var demo = function () {Alert ("Snow");}
Demo ();
Note: It is often more common to define the behavior of event properties.
4. Other Forms
var p = new Object ();
P.name = "Lisi";
p.age=31;
P.demo = Show;
Alert (P.name + ":" +p.age+demo (4,5));
Function Show (x,y) {return x+y;}
Five, array
In JavaScript, an array combines the attributes of a set, can save any element, and is variable in length. And when the assignment is not a curly brace but a bracket, as follows:
var arr = new Array ();
Arr[0] = "ASHF";
ARR[1] = 258;
or var arr = ["ASHF", 258,true, ' SFA '];
Note that when traversing, do not int x, want to Var x, weak type.
Two-dimensional array: var arrr = [[Elements, elements ... ...],[elements ...],[elements ...] .....]
six, built-in objects
In JavaScript, there are many objects built in, such as string,object,date, you can check the relevant help documents (if necessary, you can leave a mailbox), here mainly to the part of a simple description of the difference.
1.String length is a property, not a method
The substring (a,b) method in 2.String is to take a to b-1 character, and the substr (A,b) method starts with a and takes B characters
3.a.tostring (b) Returns a form of a B-system
4.parseInt ("A", B) converts a B-system to a decimal form

Vii. Custom Objects
In JavaScript, you can customize objects in addition to the built-in objects that you have already provided.
Mode one:
function car () {}
var c = new car ();
C.color = "White";
C.tyre = 4;
C.run = Show;
Function Show () {alert (c.color+c.tyre+ "Run");}
C.run ();
Mode two:
function car (Color,tyre)
{
This.color = color;
This.tyre = Tyre;
}
var c = new car ("white", 4);
Alert (c.color+ ":" +c["tyre"]);
viii. statements for manipulating objects
1.with statement
When you call multiple members of an object, to simplify the invocation, avoid the "object." Repeat writing in this format.
Format: With (object) {This code doesn't need to be added (object. Attribute)}
var p = new Car ("white", 4);
Alert (c.color+ ":" +c["tyre"]);
Can be written as:
var p = new Car ("white", 4);
With (CAR) {alert (color+ ":" +tyre)};
Note: The With statement defines the scope of an object in which members of the object can be called directly.
for...in statement
For variable in object statement: Traversal of Members in an object, if the array is traversed, the variable inside is the foot mark
Gets the value object "variable" of the object, which is the property name

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.