Table of contents
Immediately invoked function (IIFE)
Function application considerations
Immediately invoked function (IIFE)
In Javascript, parentheses () are an operator that follows a function name to call that function. For example, print() means calling the print function. How to let the function execute itself
(function(){ /* code */ }());
// 或者
(function(){ /* code */ })();
Self-executing function passing parameters
var a = 10;
var b = 20;
(function(a,b) {
console.log(a+b);
})(a,b);
eval command
The function of the eval command is to execute the string as a statement
eval('var a = 1;');
a // 1
application of functions
Implement a function that requires an array to be passed in and returns the sorted array
function mySort(arr) {
for (var i = 0; i < arr.length - 1; i++)
{
for (var j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
var temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr
}
Achieve the result of factorial: 5!
function fact(num){
if(num<=1){
return 1
}else{
return num*fact(num-1)
}
}
object overview
What is an object? Object is the core concept of JavaScript language and the most important data type
Simply put, an object is a set of "key-value pairs" (key-value), which is an unordered composite data collection
var user = {
name: 'itxiaotong',
age: '13'
};
If the key name does not meet the conditions of the identification name (such as the first character is a number, or contains spaces or operators), it must be quoted, otherwise an error will be reported
// 报错
var obj = {
1p: 'Hello World'
};
// 不报错
var obj = {
'1p': 'Hello World',
'h w': 'Hello World',
'p+q': 'Hello World'
};
Each key name of an object is also called a "property", and its "key value" can be of any data type. If the value of an attribute is a function, this attribute is usually called a "method", which can be called like a function
var user = {
getName: function (name) {
return name;
}
};
user.getName("itxiaotong") // itxiaotong
If the value of the property is still an object, a chained reference is formed
var user = {
name:"itxiaotong",
age:13,
container:{
frontEnd:["Web前端","Android","iOS"],
backEnd:["Java","Python"]
}
}
user.container.frontEnd // ["Web前端","Android","iOS"]
object properties
read properties
There are two ways to read the properties of an object, one is to use the dot operator, and the other is to use the square bracket operator
var user = {
hello: 'Hello World'
};
user.hello // "Hello World"
user['hello'] // "Hello World"
Kind tips
If you use the square bracket operator, the key name must be enclosed in quotes, otherwise it will be treated as a variable
attribute assignment
The dot operator and the square bracket operator can be used not only to read values, but also to assign values
var obj = {};
obj.name = 'itxiaotong';
obj['age'] = 13;
JavaScript allows "post-binding" of properties, that is, you can add properties at any time, there is no need to define properties when defining objects
var user = { name: "itxiaotong" };
// 等价于
var user = {};
user.name = "itxiaotong";
view all properties
To view all properties of an object itself, you can use the Object.keys method
var user = {
key1: 1,
key2: 2
};
Object.keys(user); // ['key1', 'key2']
delete command
The delete command is used to delete the properties of the object, and returns true after the deletion is successful
var user = { name: "itxiaotong" };
Object.keys(user) // ["name"]
delete user.name // true
user.name // undefined
Object.keys(user) // []
Kind tips
Delete an attribute that does not exist, delete does not report an error, and returns true
var user = {};
delete user.name // true
There is only one case where the delete command will return false, that is, the attribute exists and must not be deleted (very low usage)
for...in loop
The for...in loop is used to traverse all the properties of an object
var user = {
name:"itxiaotong",
age:13
};
for (var i in user) {
console.log(user[i]); // itxiaotong 13
}
object method
The methods of the Object object are divided into two categories: the methods of the Object itself and the instance methods of the Object
Methods of the Object object itself
The so-called "own method" is the method directly defined in the Object object
Object.print = function (info) {
console.log(info)
};
Object.print("hello");
Object 's instance methods
The so-called instance method is the method defined on the Object prototype object Object.prototype. It can be used directly by Object instance
Object.prototype.print = function (name) {
console.log(name);
};
var user = {
name:'itxiaotong'
}
user.print(user.name);
Function application considerations
function call function
There are two functions hello() and world() , so it can be run by calling another function from one function
Function body usage specification
1. The total number of lines should not exceed 50 lines
2. Different businesses are divided into different functions for processing
Requirements: Create a function, pass two parameters, and return the sum of the multiplication and addition of the two parameters
function result(x,y){
var sum = mult(x,y) + add(x,y);
console.log(sum)
}
function mult(x,y){
return x * y;
}
function add(x,y){
return x + y;
}
Math object_static property
Math is a native object of JavaScript that provides various mathematical functions. This object is not a constructor and cannot generate an instance. All properties and methods must be called on the Math object
The static properties of the Math object provide the following mathematical constants
Math.E:常数e。
Math.LN2:2 的自然对数。
Math.LN10:10 的自然对数。
Math.LOG2E:以 2 为底的e的对数。
Math.LOG10E:以 10 为底的e的对数。
Math.PI:常数 PI。
Math.SQRT1_2:0.5 的平方根。
Math.SQRT2:2 的平方根。
Kind tips
These properties are read-only and cannot be modified
Math.E // 2.718281828459045
Math.LN2 // 0.6931471805599453
Math.LN10 // 2.302585092994046
Math.LOG2E // 1.4426950408889634
Math.LOG10E // 0.4342944819032518
Math.PI // 3.141592653589793
Math.SQRT1_2 // 0.7071067811865476
Math.SQRT2 // 1.4142135623730951
achieve circle area
function circleArea(r){
return Math.PI * r * r;
}
circleArea(10); // 314.1592653589793
Math object_static method 1
Math.abs()
The Math.abs method returns the absolute value of the argument value
Math.abs(1) // 1
Math.abs(-1) // 1
Math.max(),Math.min()
The Math.max method returns the largest value among the parameters, and Math.min returns the smallest value. If the argument is empty, Math.min returns Infinity and Math.max returns -Infinity.
Math.max(2, -1, 5) // 5
Math.min(2, -1, 5) // -1
Math.min() // Infinity
Math.max() // -Infinity
Common Application Scenarios
Use the maximum and minimum values of Math to find the maximum and minimum values of the array
Math.min.apply(null,[10,20,30,40])
Math.floor(),Math.ceil()
The Math.floor method returns the largest integer less than the argument value
Math.floor(3.2) // 3
Math.floor(-3.2) // -4
The Math.ceil method returns the smallest integer greater than the argument value
Math.ceil(3.2) // 4
Math.ceil(-3.2) // -3
These two methods can be combined to implement a function that always returns the integer part of a value
function ToInteger(x) {
x = Number(x);
return x < 0 ? Math.ceil(x) : Math.floor(x);
}
ToInteger(3.2) // 3
ToInteger(3.5) // 3
ToInteger(3.8) // 3
ToInteger(-3.2) // -3
ToInteger(-3.5) // -3
ToInteger(-3.8) // -3
Math object_static method 2
Math.round()
Math.round method for rounding
Math.round(0.1) // 0
Math.round(0.5) // 1
Math.round(0.6) // 1
Kind tips
It handles negative numbers (mainly 0.5)
Math.round(-1.1) // -1
Math.round(-1.5) // -1
Math.round(-1.6) // -2
Math.pow()
The Math.pow method returns the value of the exponent raised to the base of the first argument and raised to the power of the second argument
// 等同于 2 ** 2
Math.pow(2, 2) // 4
// 等同于 2 ** 3
Math.pow(2, 3) // 8
Here's how to calculate the area of a circle
var radius = 20;
var area = Math.PI * Math.pow(radius, 2);
Math.sqrt()
The Math.sqrt method returns the square root of the argument value. If the argument is a negative value, returns NaN
Math.sqrt(4) // 2
Math.sqrt(-4) // NaN
Math.log()
The Math.log method returns the natural logarithm to base e
Math.log(Math.E) // 1
Math.log(10) // 2.302585092994046
Math.exp()
The Math.exp method returns the parameter power of the constant e
Math.exp(1) // 2.718281828459045
Math.exp(3) // 20.085536923187668