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

Introduction to JavaScript language core data types and variable usage_Basic knowledge

WBOY
Release: 2016-05-16 17:24:41
Original
919 people have browsed it

Any programming language has its own language core. Like many programming languages, JavaScript also has its own language core. The core part of the language is generally called the most basic part of JavaScript. As the saying goes, the beginning of everything is difficult, and learning JavaScript is also the same. There is an old saying that a good start is half the battle. Understanding and learning the core parts of the JavaScript language is a very good start on the path to learning JavaScript.

The following is a piece of code, which mainly introduces JavaScript data types and variables:

Copy code The code is as follows:

//In the script tag, all content after // is called annotation
//The function of annotation is generally to explain a piece of JavaScript so that other web front-end developers can Engineers may understand more clearly when reading this code

//A variable is a symbolic name of a value. Through the names of these variables, we can also generally know what this variable is used for and what it belongs to. Variable type
//It is very simple to distinguish variables. There is "var" in front of the variable, which means that the variable is declared through "var".

var m; //Declare a variable m

//Generally we assign the value to be declared to the variable with the equal sign
var m = 10; //The current variable m Equal to 10

m //Get the value just declared through the variable m

//alert(m) //Use the alert() function to pop up the value of m in the browser

//JavaScript data types: boolean, number, string, underfind, function, array, object

var n = 1; //Number
n = 0.01 //Integer sum Real numbers are all numeric types

var s = "Hello"; //A string composed of text within double quotes
s = 'Guoan'; //Consisted of text within single quotes String

var b = false; //Wrong Boolean value
b = true; //Correct Boolean value

var z = null; //A null value , is a special type. After typeof is the object

var u; //underfind

var j = { //An object representing json
li :3, //Attributes The value of "li" is 3
meng :4 //The value of attribute "meng" is 4
}

j["li"] //Access the value in json through []
j.li //Access the value in json through .
j.long = 5 //Create new attributes through assignment method
j.ai = { //You can create new json through assignment
xin : 33
}
j.kong = {} //{} represents an empty object, which has no attributes

j.ai.xin //Access the new json through. Attribute

var a = [2,3,2] //An object representing an array

a[0] //[] in the array represents the position in [], and the array starts from Starting from 0, so a[0] is the first element of the array
a.length //length represents the number of array a, 3
a[a.length-1] //represents the last element in the array An element
a[9] = 2; //Add a new element using assignment method

//If a = [], then it means that there are zero elements in the array, a.length = 0

a[0] = {
li : 333
}

//json can contain arrays, and arrays can also contain json

/ /alert(a[0]["li"])

In the above code, you can define objects through "[]", "{}", ".", or through "[ ]", "{}", "." to change the contents of the array or object. You can also read some data in the object through "[]", "{}", and ".". The following set of codes is about operators:
Copy the code The code is as follows:

//You can use budget characters in JavaScript to perform operations between two numbers and generate new values ​​
//The following are some of the more common budget characters, such as " ", "-" , "*", "/"

//1. Introducing operators
10 10 //Addition, 20
10 * 10 //Multiplication, 100
10 - 10 // Subtraction, 0
10 /10 //Division, 1

var j = { //An object representing json
li :3, //The value of attribute "li" is 3
meng :4 //The value of attribute "meng" is 4
}

j["li"] - j.meng //Attribute li in json j minus attribute meng in json j , the result is -1

"10" "10" //Addition can concatenate strings, the result is 100

//2. Some abbreviated operators are defined in JavaScript

var num = 0 //Define a number

num; //Represents self-increment, num = num 1;
num--; //Represents self-decrement, equivalent to num = num - 1;
num = 2; //Represents self-increment by 2, equivalent to num = num 2;
num *= 8; //Represents self-proclaimed 8, equivalent to num = num * 8;

//3. Operators for judgment

var a = 1,b = 2; //An equal sign represents copying, and two variables are separated by ",", which represents simultaneous declaration

a == b; //The result is false, does it mean that a and b are equal?
a != b; //The result is true, does it mean that a and b are not equal?
a < b; //The result is true, does it mean that a is less than b?
a <= b; //The result is true, does it mean that a is less than or equal to b?
a > b; //The result is false, which means Is a greater than b?
a >= b; //The result is false, which means is a greater than or equal to b?
"two" == "three"; //true "tw" index in the alphabet Greater than "th"
false > (a > b) //The result is true, which means comparing false with false

//4. Logical operators

(a = = 2) && (b == 3) //The result is true. Is a equal to 2 and b equal to 3? && means and
a > 2 || b > 2 //The result is true, the first one is false and the second one is true, because || means or
!(a == b) //The result is true. ! It means negation.

Operators that only calculate a value and do not affect any operation are called expressions and do not change the running status of the program. The statement does not contain a value, but it changes the running state. Since the statement changes the running status, a semicolon is added after it.

Each function has its own name. A certain function can be called and executed through the name. It can be defined once and called multiple times. Below is a simple example of a function.
Copy code The code is as follows:

//1. A function is a JavaScript code end with parameters. It can be defined once, called multiple times, or with parameters

var a = 3; //Declare a value Variable a is 3;

function fn1 (n) { //A function with parameter n named fn1
return n 1; //Return a value that is one greater than the value passed in
}

fn1(a) //The result is 4. Since the value of a just declared is 3, when calling the function, a 1 is executed, which is 3 1

var fOne = function(m) { //Function is also a data type, so you can also assign a variable to a function
return m*m; //Return a value and perform calculations on parameters* parameters
}

fOne(a) //The result is 9

//2. Method, assign the function to the attribute of the variable

var arr = []; //Create a new array
arr.push(1,2,3); //Use the push() method to add elements to the arr array from behind
arr.reverse(); //Use the reverse() method to sequence the elements in the array Reverse

var points = [ //Declare an array whose elements are json
{a : 0,b : 0},
{a : 1,b : 1}
]
points.dist = function () { //Define a method to calculate the distance between two points in the declared array

var p1 = this[0]; //Use this to get the current Array reference
var p2 = this[1]; //And assigned to two new variables
var a = p2.a - p1.a; //Distance on the x-axis
var b = p2.b - p1.b; //Distance on the y-axis

return Math.sqrt(a*a b*b) //Use sqrt() in Math() to calculate the square root to get two points The distance between

}

alert(points.dist()) //The result is 1.414

//3. Control statement
//Conditional statement and The loop statement is called a control statement

function abs (m) { //Find the absolute value function

if (m >= 0) { //If the comparison result is true
return m; //Return m
}else { //If the comparison result is false
return -m; //Return -m
}

}

function factorial (n) { //Function to calculate factorial

var num = 1; //Declare a variable with a value of 1

while (n > 1) { //When ( When the expression in ) is true, execute the code in loop {}

num *= n; //Equivalent to num = num * n
n--; //Equivalent to n = n -1

}

return num //Return the result of factorial

}

factorial(4) //The result is 24

function factorialFor (n) { //Use for loop to implement factorial
var i, num = 1; //Declare variable i, and declare variable num with value 1

for (i=2; i <= n ; i ) { //Loop i from 2 to n
num *= i; //Loop body, you can omit it when there is only one sentence in the loop body {}
}

return num; //Return the calculated factorial table

}

factorialFor(5)

. As can be seen from the function example, Whether it is a while loop or a for loop, whether it is a judgment statement or a loop statement, it can be regarded as a control statement. Control what will happen through certain conditions.

After introducing functions, let’s briefly introduce object-oriented.
Copy code The code is as follows:

//Defining a constructor means first creating an initialization Object

function Point (x,y) { //The first letter of the constructor name must be capitalized
this.x = x; //this represents the initialization object
this .y = y; //Save the function parameters into the properties of this initialization object
} //There is no need to return in the constructor, return something

//Use the new keyword and construct Function, create a new object
var p = new Point(1,1); //Create a point with plane coordinates (1,1)

//By assigning a value to the constructor prototype, To add a method to the newly created object of Point
Point.prototype.r = function () {

return Math.sqrt(
this.x*this.x this.y*this.y
); //Use the sqrt() method in Math to perform the square root operation. this refers to the object that calls the method

}

p.r() //The result is 1.414

The above example is to teach you how to define a request Points for square root method. There are some differences between the object orientation of JavaScript and the object orientation of other programming languages. We can only know the specific differences by continuing to study them.
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