Home > Web Front-end > JS Tutorial > Chuanzhi Podcast Learning JavaScript Basics_Basic Knowledge

Chuanzhi Podcast Learning JavaScript Basics_Basic Knowledge

WBOY
Release: 2016-05-16 18:41:51
Original
1345 people have browsed it

1. The difference between JavaScript and java
1. JavaScript is a product of Netscape, and Java is a product of Sun.
2.JavaScript is object-based and Java is object-oriented.
3. JavaScript only needs to be interpreted before it can be executed. Java needs to be compiled into a bytecode file first and then executed.
4.JavaScript is weakly typed and Java is strongly typed.
Summary: In fact, java and JavaScript have almost nothing to do with each other except that their names are similar, and JavaScript borrows some ideas from java.
2. How to combine JavaScript with Html
1. Tag form
We store JavaScript code in the tag pair <script>. . . </script>. Can be placed anywhere.
2. Import method
Use the src attribute of the script tag to import a JavaScript file.
For example:
Note: The type attribute must be added to the script tag in the specification.
3. JavaScript syntax
1. Variables
are defined by the keyword var. Since it is a weak type, there is no need to specify a specific data type.
Example: var x = 3;
2) The special constant value in JavaScript: undefined. When a variable is used without initialization, the value of the variable is undefined.
2. Operators
Like other programming languages, similar to java. It also supports the string concatenation operator ( ) and the ternary operator (? :). The difference is that the ternary operator does not need to have a value and can be directly output in the statement.
3. Statement
The statement format is similar to that of various programming languages, and there are also judgments, selections and loops. But pay attention to a few points:
1) Non-zero is true in JavaScript. For example, the following code
var x = 3;
if(x==4)//can perform comparison operations.
if(x=4)//Assignment operations can be performed, and judgments can also be made. No error is reported. The result is true, so you can write if(4==x) backwards after the if statement, so if you write 4=x, an error will be reported. Mistakes can be corrected.
2) There are no type restrictions in switch
3) The loop must have an end condition
4) To connect boolean operations, you must use && or ||, if you use & or |, bit operations will be performed

4. Function
1. General function
format:
function function name (formal parameters...)
{
execution statement;
return return value;
}
Note: 1) The return statement does not need to be written, the function must be called before it will run.
2) There is no need to add var to formal parameters, which are weak types.
3) Call a function with parameters, but do not pass it a value, or pass more values ​​than the number of parameters. The function will still run. Or call a function without parameters and pass a value to it, and the function will still run. .
4) There is no overloaded function form in JavaScript
Because multiple parameters of a function are actually encapsulated in an arguments array in JavaScript, any number of parameters can be accepted, but it is best to follow the defined form Parameters pass the actual parameters.
5) Pay attention to 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 means that show and demo represent the same function, pointing to the same object
2. Dynamic function
is implemented through JavaScript’s built-in object Function. As follows:
var demo = new Function("x,y","var sum = x y; alert(sum);");
demo(5,2);
Dynamic functions are different from general functions What's more, 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: Usually used when defining 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;}

5. Array in In JavaScript, arrays combine the characteristics of collections and can store any elements, and the length is also variable. And when assigning values, there are not braces but square brackets, as follows:
var arr = new Array();
arr[0] = "ashf";
arr[1] = 258;
Or var arr = ["ashf",258,true,'sfa'];
Be careful not to use int x when traversing, but var x, which is a weak type.
Two-dimensional array: var arrr = [[Element,Element....],[Element...],[Element....]....]

6. Built-in Object
In JavaScript, there are many built-in objects, such as String, Object, Date, etc. You can check the relevant help documents (if necessary, you can leave your email address). Here we will briefly explain some of the differences.
1. The length in String is an attribute, not a method
2. The substring (a, b) method in String takes the characters from a to b-1, while the substr (a, b) method starts from a and takes b characters
3.a.toString(b) returns the a form of b base
4.parseInt ("a", b) will convert the b base a into the decimal form of a


7. Custom objects
In JavaScript, in addition to the built-in objects already provided, you can also customize objects.
Method 1:
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();
Method 2:
function car(color, tyre)
{
this.color = color;
this.tyre = tyre;
}
var c = new car(“white”,4);
alert(c .color ":" c["tyre"]);
8. Statements for operating objects
1. with statement
When calling multiple members of an object, In order to simplify the call, avoid repeated writing in the format of "object.".
Format: with (object) { There is no need to add (object.property) to the code here}
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 directly called.
for...in statement
For variable in object statement: traverse the members in the object, if traversing the array, the variable is the subscript
Get the value object [variable] of the object, the variable is the attribute name

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