Home  >  Article  >  Web Front-end  >  javascript internal method

javascript internal method

王林
王林Original
2023-05-16 09:23:07665browse

JavaScript is a programming language widely used in web development. It provides many built-in functions and methods for working with various data types such as strings, mathematical operations, arrays, objects, dates and times. In this article, we will discuss some important internal methods of JavaScript.

1. String method

  1. indexOf(string) method: This method returns the index value of the first occurrence of the specified string in the string. If the specified string does not exist, Returns -1.

Example:

let str = "Hello World!";
let result = str.indexOf("World");
console.log(result); // 输出6
  1. slice(beginIndex, endIndex) method: This method returns a new string, including the original string from beginIndex to endIndex (exclusive) characters between. If endIndex is omitted, it goes from beginIndex to the end of the string.

Example:

let str = "Hello World!";
let result = str.slice(0, 5);
console.log(result); // 输出Hello
  1. replace(oldString, newString) method: This method replaces the specified string in the original string with a new string and returns a new character string.

Example:

let str = "Hello World!";
let result = str.replace("World", "JavaScript");
console.log(result); // 输出Hello JavaScript!

2. Mathematical methods

  1. Math.round(number) method: This method rounds the specified number to the nearest integer.

Example:

let num = 3.141592654;
let result = Math.round(num);
console.log(result); // 输出3
  1. Math.floor(number) method: This method returns the largest integer less than or equal to the specified number.

Example:

let num = 3.141592654;
let result = Math.floor(num);
console.log(result); // 输出3
  1. Math.random() method: This method returns a random number between 0 and 1.

Example:

let result = Math.random();
console.log(result); // 输出0到1之间的随机数

3. Array method

  1. push(element) method: This method adds one or more elements to the end of the array. and returns the new array length.

Example:

let array = [1, 2, 3];
let result = array.push(4, 5);
console.log(array); // 输出[1, 2, 3, 4, 5]
console.log(result); // 输出5
  1. pop() method: This method deletes the element at the end of the array and returns the deleted element.

Example:

let array = [1, 2, 3];
let result = array.pop();
console.log(array); // 输出[1, 2]
console.log(result); // 输出3
  1. splice(startIndex, deleteCount, element1, element2, ...) method: This method is used to delete elements from the array and can be replaced Or add new elements. The parameter startIndex defines the starting index of the element to be deleted. deleteCount defines the number of elements to be deleted. The following parameters element1, element2, etc. are the elements to be added to the array.

Example:

let array = [1, 2, 3, 4, 5];
let result = array.splice(2, 2, 6, 7);
console.log(array); // 输出[1, 2, 6, 7, 5]
console.log(result); // 输出[3, 4]

4. Object method

  1. Object.keys(object) method: This method returns an array containing all the keys of the object Property name.

Example:

let obj = {
  name: "Tom",
  age: 18,
  gender: "Male"
};
let result = Object.keys(obj);
console.log(result); // 输出[name, age, gender]
  1. Object.values(object) method: This method returns an array containing all property values ​​of the object.

Example:

let obj = {
  name: "Tom",
  age: 18,
  gender: "Male"
};
let result = Object.values(obj);
console.log(result); // 输出[Tom, 18, Male]
  1. Object.assign(target, source) method: This method merges the properties of multiple objects into one target object. If a property with the same name already exists in the target object, the value of the property is overwritten.

Example:

let target = {
  name: "Alice",
  age: 20
};
let source = {
  name: "Bob",
  gender: "Male"
};
let result = Object.assign(target, source);
console.log(result); // 输出{name: "Bob", age: 20, gender: "Male"}

5. Date and time methods

  1. new Date() method: This method is used to create a Date representing the current time object.

Example:

let now = new Date();
console.log(now); // 输出表示当前时间的Date对象
  1. Date.parse(string) method: This method converts a date and time string into a millisecond-level timestamp.

Example:

let timestamp = Date.parse("2022-01-01T00:00:00");
console.log(timestamp); // 输出所表示日期时间的毫秒级时间戳
  1. Date.getFullYear() method: This method returns the year representing the date.

Example:

let now = new Date();
let year = now.getFullYear();
console.log(year); // 输出当前年份

The above introduces several internal methods of JavaScript, which are important tools for writing JavaScript programs. Mastering these methods will not only help improve programming efficiency, but also help write more efficient code. In practical applications, we should choose appropriate methods according to specific needs and use them rationally.

The above is the detailed content of javascript internal method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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