函数构造器用new或不用new创建对象
2016-10-21 本文已影响0人
YHuang
// 定义
function A( x, y ) {
//如果使用new,那么this指向当前创建的新对象a1
if( !(this instanceof A) ) {
return new A( x, y )
}
this.x = x
this.y = y
}
// 使用
const a1 = new A( 1, 2 )
const a2 = A( 1, 2 )