js object oriented

Keywords: Javascript C#

1. Basic package type

To facilitate the operation of basic type values, ECMAScript provides three special reference types (basic wrapper types): Boolean, number and string.

Whenever a basic type value is read, a corresponding basic wrapper type object will be created in the background, so that some methods can be called to operate the data.

//1. Basic data types -- variables and values are stored in the stack -- no methods and properties can be called
var str='hello'; //string type
str.split("");
//Auto boxing and auto unpacking when a basic data type can call a method, the basic data type is packaged into a reference data type
str.split("");
//The basic data type has no methods and properties to call. When it can be called, it is not the basic data type, but the wrapped String type -- js automatic help
//The method in String.prototype -- automatic boxing becomes the basic data string type after calling -- automatic unpacking
Why is the basic data type not an object but can call the method of the object?
var str='hello';
str.split("");
//Background js will execute automatically
	//1. Create an instance of String type -- auto boxing
    //2. Call the specified method on the instance
    //3. Destroy this instance -- the string type of automatic unpacking has changed to string type
Call the basic wrapper type constructor with new

It is different from directly calling a conversion function with the same name

var str='100';
var str=Number(str);//Converter to convert other data types to number type
var str =new Number();//Once you use new, you create an instance Number instance that references a data type
Difference between reference type and basic wrapper class
  • Reference type: the reference type instance created with the new operator is always saved in memory before the execution flow leaves the current scope
  • Basic packing type: there is only one line of code at the moment of execution, and then it is destroyed immediately

2. String type properties and methods

  • Character method
methoddescribe
charAt(index)Returns the character at the specified index position
charCodeAt(index)Returns the character at the specified index position in Unicode encoding
  • String operation method
methoddescribe
concat()Connection string, concat(str1,str2... strx)
slice()Method extracts a part of a string and returns the extracted part as a new string. slice(start,end) does not include end. Start and end can be negative numbers. If there is no end, extract the rest
substring()Extracts the character between two specified index numbers in a string. substring(start,end) does not include end. Start and end cannot be negative numbers. If there is no end, extract the rest
substr()Extracts the specified number of characters from the string from the starting index number. substr(start,length)
  • String position method
methoddescribe
indexOf(str,n)Search the first str from n and return the index value of the search
lastIndexOf(str,n)The last str to start the search from n and return the index value of the search
  • Case conversion method
methoddescribe
toLowerCase()Converts a string to lowercase.
toUpperCase()Converts a string to uppercase.
toLocaleLowerCase()Converts a string to lowercase. localization
toLocaleUpperCase()Converts a string to uppercase. localization
  • Pattern matching method of string
methoddescribe
match()A match was found for one or more regular expressions.
replace()Replace substrings that match regular expressions.
search()Retrieves the value that matches the regular expression.
split()Splits a string into an array of strings.

3. Math object

//Common properties
Math.PI //Returns the PI (approximately equal to 3.14159).
Math.sqrt  //Return square root
//Common methods
Math.min() //Find the minimum of a set of numbers
Math.max() //Find the maximum value in a set of numbers
Math.ceil() //Round up
Math.floor() //round down
Math.round() //rounding
Math.random() //Returns a random number greater than 0 but less than 1 [0, 1)

4. Date object

Creating Date Objects
var myDate = new Date();
console.log(myDate); //2021-09-02T11:52:31.903Z
//The output is different in node environment and browser environment
console.log(myDate.getFullYear()); //2021
Common Date object methods
getFullYear()   	//Return year, such as 2020
getMonth()			//Returns the number of months in the date. The return value is 0 (January) - 11 (December)
getDate()			//Returns the number in the date object.
getHours()			//Returns the hour in the date
getMinutes()		//Returns the number of minutes in a date
getSeconds()		//Returns the number of seconds of a date
getDay()			//Returns the day of the week in the date
getMilliseconds()	//Returns the number of milliseconds in a date
getTime()			//Returns a date object in milliseconds

Posted by hmvrulz on Sat, 20 Nov 2021 04:30:49 -0800