构造器,new

  

 

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()

 

    

 

var s = {
    name: 'uiop',
    getNameFunc: function () {
        console.log(this.name)
        console.log(this)
        return function (x,y,z) {
            console.log(this===global,x,y,z)
            return this.name
        }
    }
}
b=s.getNameFunc
bb=b.apply(s)
// console.log(bb.apply(s,[1,2,3]))  // apply 传array
console.log(bb.call(s,1,2,3))  // call 传参数

 

 

function Print(){
    this.print=function(x,y){console.log(x-y,this)}
    console.log(this===global)
}

p=new Print(1,2)
console.log(p,typeof p)
p.print(1,22)
p.print.apply(p,[22,33])
p.print.call(p,22,33.4)
b=p.print
b(2,3)
console.log(Print)

 


var s = {
    name: 'uiop',
    getNameFunc: function () {
        console.log(this.name, this)
        console.log('~~~~~~~~~~~~~~~~~~~~')
        return function () {
            console.log(this === global, this)
            return this.name
        }
    }
}

console.log(s,typeof s)
b=s.getNameFunc()
console.log(b())  // b unbound
// console.log(b.call(s)) 
// console.log(b.apply(s))
console.log(b.bind(s)())
 

 

var s = {
    name: 'uiop',
    getNameFunc: function () {
        console.log(this.name, this)
        console.log('~~~~~~~~~~~~~~~~~~~~')
        return () => {  // Arrow function
            console.log(this === global, this)
            return this.name
        }
    }
}

console.log(s, typeof s)
b = s.getNameFunc()
console.log(b())  // b unbound
console.log(b.call(s)) 
console.log(b.apply(s))
// console.log(b.bind(s)())

 

 

// var s = {
//     name: 'uiop',
//     getNameFunc: function () {
//         console.log(this.name, this)
//         console.log('~~~~~~~~~~~~~~~~~~~~')
//         return () => {  // Arrow function
//             console.log(this === global, this)
//             return this.name
//         }
//     }
// }

// console.log(s, typeof s)
// b = s.getNameFunc()
// console.log(b())  // b unbound
// console.log(b.call(s)) 
// console.log(b.apply(s))
// console.log(b.bind(s)())

class s{
    constructor(){
        this.name='uiop';
        this.getNameFunc=()=>{
            console.log(this.name,this);
            console.log('~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
            return ()=>{
                console.log(this===global,this);
                return this.name;
            }
        }
    }

    jar(){
        console.log(this.name,this,this===global);
        console.log('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@');
        return ()=>{
            console.log(this.name,this,this===global);
            return this.name;
        }
    }
}

b=new s()
console.log(b,typeof b)
console.log(b.getNameFunc()())
console.log(b.jar()())

 

posted @ 2020-12-20 23:04  ascertain  阅读(119)  评论(0编辑  收藏  举报