Home  >  Article  >  Web Front-end  >  js reference type

js reference type

不言
不言Original
2018-04-26 14:37:241185browse

This article shares with you about the reference type of js. Friends in need can take a look.

The value (object) of the reference type is an instance of the reference type,, In js, a reference type is a data structure, usually called a class. es6 seems to have added the concept of class.

1: object is a basic class, and all other types inherit basic behaviors from object.

When js uses the typeof method, Array also returns object. You can use isArray() to determine whether it is an Array type.

2. Array type

Change the original The method of the array is to change the initial array on the prototype chain

push(): Add an array element to the end of the array and return the new array length

pop(): Delete The last item of the array, return to the deleted item

# Shift (): Delete the first item of the array, return to the delete item

United (): Add array elements to the front end of the array, Return the length of the array

reverse(): Reverse the order of the array

sort(): The sorting method of the array, which will call toString() of each item in the array, for Comparison, so it becomes a comparison of strings. It is best to pass it a comparison function

For example:

function compare(value1,value2){
   if(value1<value2){
       return -1;
    }else if(value1>value2){
       return 1;
    }else{
       return 0;
    }
}

If they are all numerical types, you can pass a simpler one Function

function compare(value1,value2) {
        return value2-value1;
}


concat(): Add the received parameters to the end of the array and return a new array

splice( ): Returns the deleted item, if not, returns an empty array

You can use splice() to delete, add, and add the array

Deletion: splice(0,2) deletes the first two items in the array Item

Add: splice(2,0,"red","green") Insert "red","green" items after Array[2]

Replace: splice(2 ,1,"red") Replace Array[2] with "red"

The above methods will change the following of the original array

The method will not change the original array

slice(0,2): Returns the first two items of the array

The array iteration method every(), filter(),forEach(),map(),some()

Their parameters are all a function

(item,index,array) =>{
        
 }

where every() and some() return true or false

filter() returns the array elements that satisfy the function passed in,

forEach() has no return value,

map() returns the array after calling the function

Merge method: reduce() and reduceRight()

Traverse each item of the array from left to right, reduceRight() from right to left

Accepts two parameters, one function, An initial value (optional)

For example:

let values=[1,2,3,4]
    let sun=values.reduce((prev,cur,index,arr)=>{
      return prev+cur
    },0)


function type

Function name In fact, it is also a pointer to the function object, which is no different from the variable name pointing to the object.

Change the this pointer inside the function

call(), apply() and bind()

The most important thing about call and apply is the scope in which the extension function runs

For example

window.color="red"
  let o={color:"blue"}
  function sayColor() {
      alert(this.color)
  }
  sayColor.call(this)//red
  sayColor.call(window)//red
  sayColor.call(o)//blue

bind() will create an instance of the function, and its this value will point to the value that is bound to the bind() function

bind(who), the function this will point to who

Basic packaging type

js creates corresponding basic packaging types for string, num, etc., so some methods can be called

string method

charAt( 1): Returns the character at position "1"

charCodeAt(1): Returns the character code at position "1"

concat() link string, but the most commonly used one is "+"

slice(start,end): intercept the string, end is not passed, defaults to the end position, the parameter is a negative number, converted to (character length + negative number)

substr(start,length): Intercept the string, length is not passed, the default is to the end, the parameter is a negative number and converted to (character length + negative number)

substring(start,afterend): afterend is not passed, the default is to the end, negative number is converted to 0, the smaller number will be used as the starting position

trim(): delete the spaces before and after the string

split(",") split into arrays with ","

split(",",2) takes the first two items of the split array

Math method

Math.ceil(): round up

Math .floor(): Round down

Math.round(): Round



##/ /At the end: The above may not be complete, most of them are things that I don’t have a clear impression of myself


The above is the detailed content of js reference type. 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