温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JS怎么使用function实现一个class

发布时间:2022-12-15 10:09:33 来源:亿速云 阅读:106 作者:iii 栏目:开发技术

本文小编为大家详细介绍“JS怎么使用function实现一个class”,内容详细,步骤清晰,细节处理妥当,希望这篇“JS怎么使用function实现一个class”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

正文

使用function来写出一个class的类对于我们来说当然是简单的,但必须注意的是,要做好function实现一个class,那么就要必须实现达到高精准度的模仿,把该实现的细节都实现好,才能更好的对class进行深入的理解

开始写出我们的class

class MyClass{
    constructor(name){
        this.name = name
    }
    fun(){
        console.log('myName:'+this.name)
    }
}

这是一个平平无奇的class,接着我们使用function来对其进行一下实现。

function MyClass1(name){
    this.name = name
}
MyClass1.prototype.fun = function(){
    console.log('myName:'+this.name)
}
let m1 = new MyClass1('aaa')
m1.fun() //myName:aaa

让我们继续完善一下function的实现,我们先一步一步来

Tip1:class只能通过new调用

class MyClass{
    constructor(name){
        this.name = name
    }
    fun(){
        console.log('myName:'+this.name)
    }
}
// MyClass('aaa') //TypeError: Class constructor MyClass cannot be invoked without 'new'

对于类的调用,只能通过new来进行实例化,而不能通过直接调用,下面我们在function来实现这个操作

如何判断是通过new调用而不是通过直接调用,这个时候this的熟练度就显得比较重要了,不熟悉也不要急,让我们一起来打印看看

function MyClass1(name){
    console.log(this)
    this.name = name
}
let m1 = new MyClass1('aaa') //MyClass1 {}
MyClass1('aaa') //window
  • 对于new调用,函数内部的this指向构造函数 (new绑定)

  • 直接调用 那么很明显this指向的是全局(默认绑定)

根据这个特点,在构造函数里面判断this的指向,从而抛出报错,即可实现class的效果

function MyClass1(name){
    console.log(this)
    if(!(this instanceof MyClass)){ //如果this不是指向MyClass
        throw new TypeError('TypeError: Class constructor MyClass cannot be invoked without 'new'')
    }
    this.name = name
}

Tip2:class中的函数不能被枚举

let m = new MyClass('aaa')
for(let i in m){
    console.log(i) //name
}
let m1 = new MyClass1('aaa')
for(let i in m1){
    console.log(i) //name fn
}

从上可以看出类中的函数不能被枚举出来,枚举我们就想到了enumerable,也就是Object.defineProperty来对原型上的fn进行设置

'use strict'
function MyClass1(name){
    this.name = name
}
Object.defineProperty(MyClass1.prototype,'fn',{
    value:function(){
        console.log('myName:'+this.name)
    },
    enumerable:false
})

好的,我们离完美又更近了一步。

Tip3:class中的函数不能被new调用

同样是new调用的方式,不能对类中的函数进行new调用

let m = new MyClass('aaa')
new m.fun() //TypeError: m.fun is not a constructor

在这里会抛出一个错误,所以在用function实现类的时候也要避免这个问题,function中的函数都是可以通过new来进行调用的,我们来进行修改,有了上面对new的判断经验,很容易来解决这个问题

Object.defineProperty(MyClass1.prototype,'fn',{
    value:function(){
        //不可通过new调用 
        console.log(this) //正常调用下的this是指向实例 MyClass1 { name: 'aaa' }
        if(!(this instanceof MyClass1){//那么相反 不是正常调用的就是错误的调用
            //error
        }
        console.log('myName:'+this.name)
    },
    enumerable:false
})

同样使用this的判断来看下是不是通过new的调用,正常调用下的this是指向实例 MyClass1,那么相反 不是正常调用的就是错误的调用

最后是一个小小的细节

Tip4:在ES6中使用类语法,代码都是在严格模式下运行

所以对于function的代码,需要在前面加上’user strict‘

'use strict'
function MyClass1(name){
    this.name = name
}
Object.defineProperty(MyClass1.prototype,'fn',{
    value:function(){
        console.log(this)
        if(!(this instanceof MyClass1)){
            //error
            throw TypeError('error')
        }
        console.log('myName:'+this.name)
    },
    enumerable:false
})
let m1 = new MyClass1('aaa')
m1.fn()
// new m1.fn() //error
for(let i in m1){
    console.log(i) //name
}

读到这里,这篇“JS怎么使用function实现一个class”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI