zoukankan      html  css  js  c++  java
  • js-高级03-面向对象的继承

    如何实现继承
    利用原型实现继承
    即我们可以改变一个构造函数的prototype指向,来完成继承。
    构造函数来实现
    下面中就利用了call或者apply来改变this指向的方式来实现()
    对象/函数.call(this,参数)
    对象/函数.apply(this,[参数])
    拷贝实现
    function Person (name, age) {
      this.type = 'human'
      this.name = name
      this.age = age
    }
    Person.prototype.sayName = function () {
      console.log('hello ' + this.name)
    }
    function Student (name, age) {
    //改变了自己的this的指向
      Person.call(this, name, age)
    }
    // 原型对象拷贝继承原型对象成员
    for(var key in Person.prototype) {
      Student.prototype[key] = Person.prototype[key]
    }
     
    var s1 = Student('张三', 18)
    s1.sayName() // => hello 张
    
    函数内 this 指向的不同场景
    函数的调用方式决定了 this 指向的不同:
    调用方式 非严格模式 备注
    普通函数调用 window 严格模式下是 undefined
    构造函数调用 实例对象 原型方法中 this 也是实例对象
    对象方法调用 该方法所属对象 紧挨着的对象
    事件绑定方法 绑定事件对象  
    定时器函数 window 
  • 相关阅读:
    python-day1
    go 字符串方法
    str,转换int,相互
    go 文件打包上传本地测试环境
    通联收银宝,官方文档
    go uuid
    go xid
    golang decimal处理插件包 大数字处理
    图像处理 bimg
    golang strings,常用函数
  • 原文地址:https://www.cnblogs.com/adylz111/p/13433751.html
Copyright © 2011-2022 走看看