构造器,new

标签: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

版权声明:完美者 发表于 2020-12-25 12:26:46。
转载请注明:构造器,new | 完美导航

暂无评论

暂无评论...