Number的类 、Symbol的部分类方法 、Fuction

Number的类 、Symbol的部分类方法 、Fuction的部分介绍


一、Number的类属性

类属性(值属性)名称
Number.NaNnot 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} }`;
                }
            }

因为 introduce 是个箭头函数,因此 通过 x.introduce 调用该函数时,其内部的 this 指向上下文中已经存在的 this。

  • 20
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值