class和构造函数

 

/*ES6class*/
/*class实际是构造函数语法糖,是对构造函数的一层包装,是为了容易理解*/
/*class与构造函数共同点:所有的方法都在原型上*/
class Point1 {
  count=6 /*属于实列的属性,不在原型上,不会被继承*/
  constructor(x, y) {
    this.x = x; /*this指向实例对象,x,y在对象自身,不在原型上*/
    this.y = y;
  }

  toString() {
    return '(' + this.x + ', ' + this.y + ')';
  }
}
var p1 = new Point1(1, 2);
console.log("class====",p1)
console.log(Object.keys(Point1.prototype))
/*ES5构造函数*/
//不同点class里面的不可以枚举,es5中除去构造函数,其他都可以枚举
function Point(x, y) {
  this.x = x;
  this.y = y;
}

Point.prototype.toString = function () {
  return '(' + this.x + ', ' + this.y + ')';
};

var p = new Point(1, 2);
console.log("es5===",p)
console.log(Object.keys(Point.prototype))

new 做的事情

1.先创建一个对象

2.将目前作用域赋值给这个对象,即this指向当前对象

3.执行构造函数

4.返回一个对象。如果构造函数没有返回对象的情况下,就返回当前对象

转载于:https://www.cnblogs.com/sisi2020/p/10457992.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值