美文网首页
JS object创建方式

JS object创建方式

作者: smile丶ywx | 来源:发表于2019-06-08 17:48 被阅读0次
        //原型链 new方式对象创建
        let Obj = function() {
            this.color = 'red'
        }
    
        Obj.prototype = {
            getColor:function() {
                return this.color
            }
        }
    
        let obj = new Obj()
    
        //直接申明
        let obj = {
            color: 'red',
            getColor:function() {
                return this.color
            }
        }
    
        //Object.create()
        let obj = Object.create({
            color: 'red',
            getColor:function() {
                return this.color
            }
        })
    

    相关文章

      网友评论

          本文标题:JS object创建方式

          本文链接:https://www.haomeiwen.com/subject/onnixctx.html