js对象实例的相关方法

class People {
    constructor(name) {
      this.name = name,
          this.car = 'benz'
      this.sing = function(){}
      console.log('singing');
    }
    //外部的eat相对于手动继承写 People.prototype.eat=function(){}
    eat() {
      console.log('eat something...');
    }
  }
  //改变People.prototype.eat(),会改变内部的class内部的eat
  People.prototype.eat = function () {
    console.log('eat apple');
  }
  class Student extends People {
    constructor(name, age) {
      super(name)//
      //相当于 手动继承方式中的People.prototype.constructor.call(this,name) OR People.call(this,name)
      this.age = age
      this.car = 'baomao'
    }
    sayHi() {
      console.log(
          this.name,this.age
      );
    }

  }
  const XIALUO = new Student('夏洛', '13')

  let xialuo = XIALUO
  1. for in
  2. hasOwnProperty
    这两个都是考虑对象自身的属性和方法,通过People.prototype.constructor.call(this) 继承的属性和方法也称作自己的属性和方法,比如name sing
    而eat并不是,他是People.prototype的方法。
 xialuo.sing()//singing不是本身的属性而是它的原型链上的。
  for (let k in xialuo){
    console.log(k,':',xialuo[k]);//只能是构造器的属性和方法
  }
 //name : 夏洛
 // car : baomao
 // sing : ƒ (){}
 // age : 13
  console.log(xialuo.hasOwnProperty('name'));//true
  console.log(xialuo.hasOwnProperty('age'));//true
  console.log(xialuo.hasOwnProperty('eat'));//false
  1. 待续
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值