The simple use and operation of TypeScript objects include number objects, etc.

1. TypeScript object

1. An object is an instance that contains a set of key-value pairs

let oneObj = {
    
     
    key1: "1", 
    key2: "2",  
    key3:["content1", "content2"] 
}

2.TypeScript type template
In js, we can add new values ​​to objects oneObj.key4 = [2,3], but this is not allowed in ts

类型“{
    
     key1: string; key2: string; key3: string; }”上不存在属性“key4”

When we change a certain value of the object, we can use oneObj.key3 = 2; but this will also cause compilation errors in ts.
That is: Typescript 中的对象必须是特定类型的实例
for example:

let oneObj = {
    
     
    key1: "1", 
    key2: "2",
    key3: "",
}
oneObj.key3 = "2";
console.log(oneObj) //{ key1: '1', key2: '2', key3: '2' }

3. Duck type
The effective semantics of an object does not inherit a specific class or implement a specific interface, but is determined by "the current collection of methods and attributes".
" 当看到一只鸟走起来像鸭子、游泳起来像鸭子、叫起来也像鸭子,那么这只鸟就可以被称为鸭子."

interface  oneDuck {
    
    
    speak:string,
    walk:string,
}
function doDuck(d1:oneDuck,d2:oneDuck):oneDuck {
    
     
    let speak = d1.speak + d2.speak 
    let walk = d1.walk + d2.walk 
    return {
    
    speak:speak,walk:walk} 
} 
// 正确
let newDuck = doDuck({
    
    speak:"hello",walk:"go"},{
    
    speak:"ts",walk:"go"})  
 console.log(newDuck) //{ speak: 'hellots', walk: 'gogo' }
// 错误 
let newDuck2 =doDuck({
    
    speak:"hello"},{
    
    speak:"ts",walk:"go"})

Note: \color{red}{Note:}Note: The object containing speak and walk can be called a duck. It is OK to pass in two ducks in newDuck, but the first duck passed in newDuck2 has no walk, which means it is a fake duck. will cause a compile-time error when running, but in regular functions, it is acceptable to accept an object of type "fake duck".
For example:

function oneDuck(d1:any,d2:any) {
    
     
    let speak = d1.speak + d2.speak 
    let walk = d1.walk + d2.walk 
    console.log({
    
    speak:speak,walk:walk} ) 
} 
oneDuck({
    
    speak:"hello"},{
    
    speak:"ts",walk:"go"})

2. TypeScript Number object

1. The properties contained in the Number object

Attributes describe
MAX_VALUE Indicates the largest number, the MAX_VALUE attribute value is close to 1.79E+308. Values ​​greater than MAX_VALUE represent "Infinity"
MIN_VALUE Indicates the smallest number, that is, the positive number closest to 0 (in fact, it will not become 0). The largest negative number is -MIN_VALUE, and the value of MIN_VALUE is about 5e-324. Values ​​less than MIN_VALUE ("underflow values") will be converted to 0.
NaN Not-A-Number (Not-A-Number)
NEGATIVE_INFINITY Negative infinity, which is returned on overflow. The value is less than MIN_VALUE.
POSITIVE_INFINITY Positive infinity, which is returned on overflow. The value is greater than MAX_VALUE.
prototype Static property of the Number object. Properties and methods can be added to objects.
constructor Returns a reference to the Number function that created this object.
console.log(oneNum.constructor)
console.log("TypeScript Number 属性: "); 
console.log("最大值为: " + Number.MAX_VALUE); 
console.log("最小值为: " + Number.MIN_VALUE); 
console.log("负无穷大: " + Number.NEGATIVE_INFINITY); 
console.log("正无穷大:" + Number.POSITIVE_INFINITY);
console.log("NaN:" + Number.NaN);
console.log(Number.constructor);
// [Function: Number]
// TypeScript Number 属性: 
// 最大值为: 1.7976931348623157e+308
// 最小值为: 5e-324
// 负无穷大: -Infinity
// 正无穷大:Infinity
// NaN:NaN
// [Function: Function]

Use of prototype:

function onePeopleFun(this:any,num:number,name:string) {
    
     
    this.num = num 
    this.name = name 
} 
let onePeople = new (onePeopleFun as any)(123,"admin") 
onePeopleFun.prototype.email = "[email protected]" 
 
console.log(onePeople.num) 
console.log(onePeople.name) 
console.log(onePeople.email) 

2. Methods of the Number object

Attributes describe
toExponential() Converts the value of an object to exponential notation.
toFixed() Converts a number to a string, specifying the number of digits for the decimal point.
toLocaleString() Converts a number to a string, using the native number format order.
toPrecision() Formats a number to the specified length as a string.
toString() Converts a number to a string, using the specified radix. The base of a number is an integer between 2 and 36. If this parameter is omitted, base 10 is used.
valueOf() Returns the raw numeric value of a Number object.

3. Simple use of Number objects

let oneVal:number = 1234.1234;
let oneNumb = new Number(oneVal);
console.log(oneNumb.toExponential())
console.log(oneNumb.toFixed(2))
console.log(oneNumb.toFixed(6))
console.log(oneNumb.toLocaleString())
console.log(oneNumb.toPrecision(5))
console.log(oneNumb.toPrecision(6))
console.log(oneNumb.toString());  // 输出10进制
console.log(oneNumb.toString(2)); // 输出2进制
console.log(oneNumb.toString(8)); // 输出8进制
console.log(oneNumb.valueOf());
let twoNumb = new Number("2");
console.log(twoNumb.valueOf());
// 1.2341234e+3
// 1234.12
// 1234.123400
// 1,234.123
// 1234.1
// 1234.12
// 1234.1234
// 10011010010.00011111100101110010010001110100010100111
// 2322.07713444350516
// 1234.1234
// 2

3. TypeScript String object

1. The properties contained in the String object

属性 描述
length Returns the length of the string.
prototype Static properties of String objects. Properties and methods can be added to objects.
constructor Returns a reference to the String function that created this object.
let oneStr = new String("Hello TypeScript");
console.log(oneStr.constructor)
console.log(oneStr.length)
// [Function: String]
// 16
// prototype使用同上

2. The methods contained in the String object

属性 描述
charAt() Returns the character at the specified position.
charCodeAt() Returns the Unicode encoding of the character at the specified position.
concat() Concatenates two or more strings and returns the new string.
indexOf() Returns the position within a string of the first occurrence of a specified string value.
lastIndexOf() Searches the string backwards and forwards, and returns the last occurrence of the string, counting from the start position (0).
localeCompare() Compares two strings using locale-specific ordering.
match() Find finds a match for one or more regular expressions.
replace() Replace substrings that match the regular expression.
search() Retrieves values ​​that match the regular expression.
slice() Extracts a segment of a string and returns the extracted portion in a new string.
split() Split a string into an array of substrings.
substr() Extracts the specified number of characters in a string from the starting index number.
substring() Extracts the characters in a string between two specified index numbers.
toLocaleLowerCase() Converts strings to lowercase according to the locale of the host, only a few languages ​​(such as Turkish) have locale-specific case mappings.
toLocaleUpperCase() Convert strings to uppercase according to the host's locale, only a few languages ​​(such as Turkish) have locale-specific case mappings.
toLowerCase() Convert a string to lowercase.
toString() Returns a string.
toUpperCase() Convert a string to uppercase.
valueOf() Returns the primitive value of the specified string object.

3. Simple use of String objects

console.log("str.charAt(0) 为:" + oneStr.charAt(0));
console.log("str.charCodeAt(0) 为:" + oneStr.charCodeAt(0));
let str1 = new String("Hello");
let str2 = new String("TypeScript");
let str3 = str1.concat(str2 as string);
console.log("str1 + str2 : " + str3) 
// str.charAt(0) 为:H
// str.charCodeAt(0) 为:72
// str1 + str2 : HelloTypeScript
console.log(oneStr.indexOf( "l" ))
console.log(oneStr.lastIndexOf("l" ))
// 2
// 3
console.log(oneStr.localeCompare("Hello"))
console.log(oneStr.localeCompare("TypeScript"))
console.log(oneStr.localeCompare("Hello TypeScript"))
// 1
// -1
// 0
console.log(oneStr.match(/l/g)) 
console.log(oneStr.search(/a/g)) 
console.log(oneStr.search(/l/g)) 
// [ 'l', 'l' ]
// -1
// 2
console.log(oneStr.slice(2)) // llo TypeScript
console.log(oneStr.split(" ", 1)) // [ 'Hello' ]
console.log(oneStr.split(" ", 2)) // [ 'Hello', 'TypeScript' ]
console.log(oneStr.substring(1,3)) // el
console.log(oneStr.toLocaleLowerCase()) // hello typescript
console.log(oneStr.toLocaleUpperCase()) // HELLO TYPESCRIPT
console.log(oneStr.toLowerCase()) // hello typescript
console.log(oneStr.toUpperCase()) // HELLO TYPESCRIPT
let oneStr2:number = 123;
console.log(oneStr2.toString()) // "123"
let oneStr3 = new String(oneStr2); 
console.log(oneStr3.valueOf( ));// "123"

Note: \color{red}{Note:}Note: Two parameters can be passed in the split, the first is what is the target to cut, the second is to control the length of the returned array, or only one parameter can be passed

Guess you like

Origin blog.csdn.net/samxiaoguai/article/details/128371313