码迷,mamicode.com
首页 > 其他好文 > 详细

构造器,new

时间:2020-12-25 12:26:46      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:loading   src   color   func   point   call   extends   fun   show   

  

 

ES6之前,定义一个函数(构造器)对象,使用this定义属性

使用new & 构造器创建一个新对象

  

function B(x){
    console.log(B class)
    console.log(this);
    this.x=x;
    this.show=function(){console.log(3333333)}
}

console.log(typeof B)

b=new B(22)
b.show()
console.log(typeof b,b)

function Point(x,y){
    this.x=x;
    this.y=y;
    this.show=()=>console.log(this,this.x,this.y)
    console.log(Point ~~~~~~~~~~~~)
}

p=new Point(4,8)
p.show()

 

function Point(x,y){
    console.log(this)
    this.x=x;
    this.y=y;
    console.log(this)
    this.show=()=>console.log(this,this.x,this.y)
}

p1=new Point(4,9)
console.log(p1)

技术图片

 

 

 

function Point(x,y){
    this.x=x;
    this.y=y;
    this.show=()=>console.log(this,this.x,this.y)
    console.log(this)
    console.log(Point ~~~~~~~~~~~~~~)
}

// p1=new Point(4,9)
// console.log(Point)
// console.log(p1)
// p1.show()

// inherit
function Point3D(x,y,z){
    Point.call(this,x,y)
    this.z=z;
    console.log(Point3d !!!!!!!!!!!!!!!!!!!!)
}

console.log(Point3D)
p2=new Point3D(8,4,3)
console.log(p2)
p2.show()
console.log(Point.call)
console.log(p2.call)

 

new 构建一个新的通用对象,new操作符会将新对象的this值传递给Point3D构造器函数,函数为这个对象创建z属性

new后得到一个对象,使用这个对象的this来调用构造器,使用Point3D对象的this来执行Point的构造器,所以使用call方法,传入子类this

 

class Point{
    constructor(x,y){
        this.x=x;
        this.y=y;
        this.bb=function(){console.log(Point ,this.x,this.y,this.z)}
        // this.show=()=>console.log(‘Point vv‘)
        console.log(Point ~~~~~~~~)
    }
    show(){
        console.log(this,this.x,this.y)
    }
}

// console.log(Point)
// p1=new Point(5,9)
// console.log(p1)
// p1.show()

class Point3D extends Point{
    constructor(x,y,z){
        super(x,y)
        this.z=z;
        this.bb=function(){console.log(Point3D ,this)}
        this.vv=()=>console.log(Point3D vv)
    }

    show(){
        // console.log(this,this.x,this.y,this.z)
        super.show()
        console.log(this.z)
    }

    static print(){
        return static print
    }
}

console.log(Point3D)
p2=new Point3D(2,44,88)
console.log(p2)
p2.show()
console.log(Point3D.print())
// Point3D.show()
p2.constructor.print()
// p2.bb()
// p2.vv()

 

    

构造器,new

标签:loading   src   color   func   point   call   extends   fun   show   

原文地址:https://www.cnblogs.com/dissipate/p/14165844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!