Number的类 、Symbol的部分类方法 、Fuction的部分介绍
一、Number的类属性
类属性(值属性) | 名称 |
---|---|
Number.NaN | not a number |
Number.NEGATIVE_INFINITY | 负无穷大 |
Number.POSITIVE_INFINITY | 负无穷小 |
Number.MAX_VALUE | 最大值 |
Number.MIN_VALUE | 最小值 |
Number.MAX_SAFE_INTEGER | 最大安全整数值 |
Number.MIN_SAFE_INTEGER | 最小安全整数值 |
Number.EPSILON | 可表示的大于 1 的最小的浮点数之间的差值。 |
1.判断参数是否是有穷大数字
console.log( Number.isFinite( Number.MAX_SAFE_INTEGER ) );
true
console.log( Number.isFinite( Number.NEGATIVE_INFINITY ) );
false
2.判断参数是否是有穷大数字
console.log( Number.isInteger( 3.14 ) );
false
console.log( Number.isInteger( Number.MIN_SAFE_INTEGER ) );
3.判断参数是否是安全整数
console.log( Number.isSafeInteger( Number.MIN_SAFE_INTEGER - 1 ) ); // false
console.log( Number.isSafeInteger( Number.MAX_SAFE_INTEGER + 1 ) ); // false
对于超出 [ Number.MIN_SAVE_INTEGER, Number.MAX_SAFE_INTEGER ] 范围的整数应该使用 bigint 来表示.
let bigInteger = BigInt(Number.MAX_SAFE_INTEGER) ** 2n;
console.log( bigInteger , typeof bigInteger );
4.判断参数是否不是数字
console.log( Number.isNaN(undefined) ); // false
console.log( Number.isNaN(null) ); // false
console.log( Number.isNaN(NaN)); // true
console.log( Number.isNaN(Number.NEGATIVE_INFINITY)); // false
关于 undefined 的判断,Number.isNaN 与 globalThis.isNaN 是不同的
console.log( Number.parseInt('0x12345') );
console.log( Number.parseInt('12345', 8 ) );
console.log( Number.parseInt('12345', 16 ) );
console.log( Number.parseInt('12345', 36 ) );
console.log( Number.parseFloat('1234.5678') );
二、Number的原型属性
1.Number.prototype.toFixed(digits)
用定点表示法来格式化一个数值 (参数值的范围是[0, 100])
let h = x.toFixed(2);
console.log( h, typeof h );
h = x.toFixed(5);
console.log( h, typeof h );
2. Number.prototype.toPrecision(precision)
以指定的精度返回该数值对象的字符串形式
let k = x.toPrecision(10);
console.log( k, typeof k );
3. Number.prototype.toExponential(fractionDigits)
以指数表示法返回该数值字符串形式
console.log( x.toExponential(3) );
console.log( y.toExponential(3) );
4. Number.prototype.valueOf()
获取Number实例中所包裹的原始值
let t = x.valueOf();
console.log( t, typeof t );
5. Number.prototype.toString()
let s = x.toString();
console.log( s, typeof s );
6. Number.prototype.toLocaleString()
s = x.toLocaleString();
console.log( s, typeof s );
三、Symbol的部分类方法(for/keyFor)
1. Symbol.for(key)
会根据给定的键 key,来从运行时的 symbol 注册表中找到对应的 symbol,如果找到了,则返回它,否则,新建一个与该键关联的 symbol,并放入全局 symbol 注册表中。
let idSymbol = Symbol.for('id');
console.log( idSymbol, typeof idSymbol );
let another = Symbol.for('id');
console.log( another, typeof another );
console.log( another === idSymbol ); // true
2. Symbol.keyFor(symbol)
用来获取全局 symbol 注册表中与某个 symbol 关联的键
let key = Symbol.keyFor(idSymbol);
console.log( key );
四、Fuction
1.Fuction原型属性
属性 | 解释 |
---|---|
Function.prototype.name | 属性返回函数实例的名称。 |
Function.prototype.length | 函数期望的参数数量。 |
Function.prototype.constructor | |
Function.prototype.toString |
console.log( 'name: ', Function.prototype.name );
console.log( 'length: ', Function.prototype.length );
console.log( 'constructor: ', Function.prototype.constructor );
console.log( 'toString: ', Function.prototype.toString );
因为所有函数都是Function的实例,因此每个函数都从Function.prototype继承属性
console.log( welcome.constructor === Function.prototype.constructor ); // true
console.log( welcome.constructor === Function ); // true
判断 welcome 本身是否拥有一个名称是 constructor 的自有属性
console.log( Object.hasOwn( welcome, 'constructor' ) ); // false
每个函数都有一个name属性用于获取其名称(这与Function.prototype.name无关)
console.log( Object.hasOwn( welcome, 'name' ) ); // true
每个函数都有一个length属性用于获取其"参数个数"(这与Function.prototype.length无关)
console.log( Object.hasOwn( welcome, 'length' ) ); // true
Function.prototype.toString():
let code = welcome.toString();
console.log( code);
2. Function.prototype.call( thisArg, ...params )
通过 call 来调用函数
welcome.call( null, '小芳' );
当直接用函数名调用函数时,函数内部的 this
指向 global object
,而通过call调用函数时,若call首个参数为 null 或 undefined 或 未显式指定,则函数内部的 this
指向 global object
。
introduce.call( null );
introduce.call( undefined );
introduce.call();
而/ 将 x 作为参数传递给 call 函数, 则在调用 introduce 函数时,其内部的 this 就指向了 x 对应的实例
introduce.call( x );
当通过定义方法的实例调用方法时,方法内部的this指向定义该方法的实例。但是通过 call 调用 instance.fly 方法时,为 call 函数传入的第一个参数就是 instance.fly 方法内部this指向的实例
instance.fly.call(x);
3、Function.prototype.apply( thisArg [, paramArray] )
当直接用函数名调用函数时,函数内部的 this
指向 global object
,但通过apply调用函数时,若apply首个参数为 null 或 undefined 或 未显式指定,则函数内部的 this
指向 global object
introduce.apply( null );
introduce.apply( undefined );
introduce.apply();
const x = { name: '赵德芳' }
所以将 x 作为参数传递给 apply 函数,则在调用 introduce 函数时,其内部的 this 就指向了 x 对应的实例。
introduce.apply( x );
4.箭头函数使用this
const x = {
name: '德芳',
// 将一个箭头函数作为属性值
introduce: () => {
// 箭头函数中的 this 就是该箭头函数所在作用域中已经绑定过的 this
console.log(`我是${this.name}`);
},
// 定义方法(本质上是定义了一个函数)
toString(){
return `{ name: ${this.name} }`;
}
}
const x = {
name: '德芳',
// 将一个箭头函数作为属性值
introduce: () => {
// 箭头函数中的 this 就是该箭头函数所在作用域中已经绑定过的 this
console.log(`我是${this.name}`);
},
// 定义方法(本质上是定义了一个函数)
toString(){
return `{ name: ${this.name} }`;
}
}