当前位置:   article > 正文

JavaScript ES6实现继承_es6继承

es6继承

1 对象的方法补充

2 原型继承关系图

3 class方式定义类

4 extends实现继承

5 extends实现继承

6 多态概念的理

function 创建的名称如果开头是大写的,那这个创建的不是函数,是创建了类。

 ES6-class类中的内容

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. var obj = {
  12. running: function() {},
  13. eating: () => {},
  14. swimming() {
  15. }
  16. }
  17. // function Person() {
  18. // }
  19. // Person.prototype.running = function() {
  20. // }
  21. // 编程: 高内聚低耦合
  22. class Person {
  23. // 1.类中的构造函数
  24. // 当我们通过new关键字调用一个Person类时, 默认调用class中的constructor方法
  25. constructor(name, age) {
  26. this.name = name
  27. this.age = age
  28. }
  29. // 2.实例方法
  30. // 本质上是放在Person.prototype
  31. running() {
  32. console.log(this.name + " running~")
  33. }
  34. eating() {
  35. console.log(this.name + " eating~")
  36. }
  37. }
  38. // 创建实例对象
  39. var p1 = new Person("why", 18)
  40. // 使用实例对象中属性和方法
  41. console.log(p1.name, p1.age)
  42. p1.running()
  43. p1.eating()
  44. // 研究内容
  45. console.log(Person.prototype === p1.__proto__)
  46. console.log(Person.running) // 不能调用
  47. console.log(Person.prototype.running) // 可以调用
  48. </script>
  49. </body>
  50. </html>

ES6-class和function类的区别

可以把class创建的类当做是function创建的类的一种语法糖。但是在直接使用的方面是有不同之处。类里面的方法又叫静态方法。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // function定义类
  12. function Person1(name, age) {
  13. this.name = name
  14. this.age = age
  15. }
  16. Person1.prototype.running = function() {}
  17. Person1.prototype.eating = function() {}
  18. var p1 = new Person1("why", 18)
  19. console.log(p1.__proto__ === Person1.prototype)
  20. console.log(Person1.prototype.constructor)
  21. console.log(typeof Person1) // function
  22. // 不同点: 作为普通函数去调用
  23. Person1("abc", 100)
  24. // class定义类
  25. class Person2 {
  26. constructor(name, age) {
  27. this.name = name
  28. this.age = age
  29. }
  30. running() {}
  31. eating() {}
  32. }
  33. var p2 = new Person2("kobe", 30)
  34. console.log(p2.__proto__ === Person2.prototype)
  35. console.log(Person2.prototype.constructor)
  36. console.log(typeof Person2)
  37. // 不同点: class定义的类, 不能作为一个普通的函数进行调用
  38. Person2("cba", 0)
  39. </script>
  40. </body>
  41. </html>

ES6-对象访问器方法的编写

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 针对对象
  12. // 方式一: 描述符
  13. // var obj = {
  14. // _name: "why"
  15. // }
  16. // Object.defineProperty(obj, "name", {
  17. // configurable: true,
  18. // enumerable: true,
  19. // set: function() {
  20. // },
  21. // get: function() {
  22. // }
  23. // })
  24. // 方式二: 直接在对象定义访问器
  25. // 监听_name什么时候被访问, 什么设置新的值
  26. var obj = {
  27. _name: "why",
  28. // setter方法
  29. set name(value) {
  30. this._name = value
  31. },
  32. // getter方法
  33. get name() {
  34. return this._name
  35. }
  36. }
  37. obj.name = "kobe"
  38. console.log(obj.name)
  39. </script>
  40. </body>
  41. </html>

ES6-类的访问器方法的编写

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 1.访问器的编写方式
  12. // class Person {
  13. // // 程序员之间的约定: 以_开头的属性和方法, 是不在外界访问
  14. // constructor(name, age) {
  15. // this._name = name
  16. // }
  17. // set name(value) {
  18. // console.log("设置name")
  19. // this._name = value
  20. // }
  21. // get name() {
  22. // console.log("获取name")
  23. // return this._name
  24. // }
  25. // }
  26. // var p1 = new Person("why", 18)
  27. // p1.name = "kobe"
  28. // console.log(p1.name)
  29. // // console.log(p1._name)
  30. // var p2 = new Person("james", 25)
  31. // console.log(p2.name)
  32. // 2.访问器的应用场景
  33. class Rectangle {
  34. constructor(x, y, width, height) {
  35. this.x = x
  36. this.y = y
  37. this.width = width
  38. this.height = height
  39. }
  40. get position() {
  41. return { x: this.x, y: this.y }
  42. }
  43. get size() {
  44. return { width: this.width, height: this.height }
  45. }
  46. }
  47. var rect1 = new Rectangle(10, 20, 100, 200)
  48. console.log(rect1.position)
  49. console.log(rect1.size)
  50. </script>
  51. </body>
  52. </html>

ES6-类的静态方法的编写

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // function Person() {}
  12. // // 实例方法
  13. // Person.prototype.running = function() {}
  14. // // 类方法
  15. // Person.randomPerson = function() {}
  16. // var p1 = new Person()
  17. // p1.running()
  18. // Person.randomPerson()
  19. // class定义的类
  20. var names = ["abc", "cba", "nba", "mba"]
  21. class Person {
  22. constructor(name, age) {
  23. this.name = name
  24. this.age = age
  25. }
  26. // 实例方法
  27. running() {
  28. console.log(this.name + " running~")
  29. }
  30. eating() {}
  31. // 类方法(静态方法)
  32. static randomPerson() {
  33. console.log(this)
  34. var randomName = names[Math.floor(Math.random() * names.length)]
  35. return new this(randomName, Math.floor(Math.random() * 100))
  36. }
  37. }
  38. var p1 = new Person()
  39. p1.running()
  40. p1.eating()
  41. var randomPerson = Person.randomPerson()
  42. console.log(randomPerson)
  43. </script>
  44. </body>
  45. </html>

ES6-通过extends实现继承

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 定义父类
  12. class Person {
  13. constructor(name, age) {
  14. this.name = name
  15. this.age = age
  16. }
  17. running() {
  18. console.log("running~")
  19. }
  20. eating() {
  21. console.log("eating~")
  22. }
  23. }
  24. class Student extends Person {
  25. constructor(name, age, sno, score) {
  26. // this.name = name
  27. // this.age = age
  28. super(name, age)
  29. this.sno = sno
  30. this.score = score
  31. }
  32. // running() {
  33. // console.log("running~")
  34. // }
  35. // eating() {
  36. // console.log("eating~")
  37. // }
  38. studying() {
  39. console.log("studying~")
  40. }
  41. }
  42. var stu1 = new Student("why", 18, 111, 100)
  43. stu1.running()
  44. stu1.eating()
  45. stu1.studying()
  46. class Teacher extends Person {
  47. constructor(name, age, title) {
  48. // this.name = name
  49. // this.age = age
  50. super(name, age)
  51. this.title = title
  52. }
  53. // running() {
  54. // console.log("running~")
  55. // }
  56. // eating() {
  57. // console.log("eating~")
  58. // }
  59. teaching() {
  60. console.log("teaching~")
  61. }
  62. }
  63. </script>
  64. </body>
  65. </html>

ES6-super关键字的其他用法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. class Animal {
  12. running() {
  13. console.log("running")
  14. }
  15. eating() {
  16. console.log("eating")
  17. }
  18. static sleep() {
  19. console.log("static animal sleep")
  20. }
  21. }
  22. class Dog extends Animal {
  23. // 子类如果对于父类的方法实现不满足(继承过来的方法)
  24. // 重新实现称之为重写(父类方法的重写)
  25. running() {
  26. console.log("dog四条腿")
  27. // 调用父类的方法
  28. super.running()
  29. // console.log("running~")
  30. // console.log("dog四条腿running~")
  31. }
  32. static sleep() {
  33. console.log("趴着")
  34. super.sleep()
  35. }
  36. }
  37. var dog = new Dog()
  38. dog.running()
  39. dog.eating()
  40. Dog.sleep()
  41. </script>
  42. </body>
  43. </html>

继承自内置类的用法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 1.创建一个新的类, 继承自Array进行扩展
  12. class HYArray extends Array {
  13. get lastItem() {
  14. return this[this.length - 1]
  15. }
  16. get firstItem() {
  17. return this[0]
  18. }
  19. }
  20. var arr = new HYArray(10, 20, 30)
  21. console.log(arr)
  22. console.log(arr.length)
  23. console.log(arr[0])
  24. console.log(arr.lastItem)
  25. console.log(arr.firstItem)
  26. // 2.直接对Array进行扩展
  27. Array.prototype.lastItem = function() {
  28. return this[this.length - 1]
  29. }
  30. var arr = new Array(10, 20, 30)
  31. console.log(arr.__proto__ === Array.prototype)
  32. console.log(arr.lastItem())
  33. // 函数apply/call/bind方法 -> Function.prototype
  34. </script>
  35. </body>
  36. </html>

ES6-类的混入mixin的用法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // JavaScript只支持单继承(不支持多继承)
  12. function mixinAnimal(BaseClass) {
  13. return class extends BaseClass {
  14. running() {
  15. console.log("running~")
  16. }
  17. }
  18. }
  19. function mixinRunner(BaseClass) {
  20. return class extends BaseClass {
  21. flying() {
  22. console.log("flying~")
  23. }
  24. }
  25. }
  26. class Bird {
  27. eating() {
  28. console.log("eating~")
  29. }
  30. }
  31. // var NewBird = mixinRunner(mixinAnimal(Bird))
  32. class NewBird extends mixinRunner(mixinAnimal(Bird)) {
  33. }
  34. var bird = new NewBird()
  35. bird.flying()
  36. bird.running()
  37. bird.eating()
  38. </script>
  39. </body>
  40. </html>

ES6-ES6中的class转ES5代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // class Person {
  12. // constructor(name, age) {
  13. // this.name = name
  14. // this.age = age
  15. // }
  16. // running() {}
  17. // eating() {}
  18. // static randomPerson() {}
  19. // }
  20. // var p1 = new Person()
  21. </script>
  22. <script src="./js/es5_code01.js"></script>
  23. </body>
  24. </html>

可以去babel官网打开try out,然后改default。

ES6-Java面向对象的多态理解

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 继承是多态的前提
  12. // shape形状
  13. class Shape {
  14. getArea() {}
  15. }
  16. class Rectangle extends Shape {
  17. constructor(width, height) {
  18. super()
  19. this.width = width
  20. this.height = height
  21. }
  22. getArea() {
  23. return this.width * this.height
  24. }
  25. }
  26. class Circle extends Shape {
  27. constructor(radius) {
  28. super()
  29. this.radius = radius
  30. }
  31. getArea() {
  32. return this.radius * this.radius * 3.14
  33. }
  34. }
  35. var rect1 = new Rectangle(100, 200)
  36. var rect2 = new Rectangle(20, 30)
  37. var c1 = new Circle(10)
  38. var c2 = new Circle(15)
  39. // 表现形式就是多态
  40. /*
  41. 在严格意义的面向对象语言中, 多态的是存在如下条件的:
  42. 1.必须有继承(实现接口)
  43. 2.必须有父类引用指向子类对象
  44. */
  45. function getShapeArea(shape) {
  46. console.log(shape.getArea())
  47. }
  48. getShapeArea(rect1)
  49. getShapeArea(c1)
  50. var obj = {
  51. getArea: function() {
  52. return 10000
  53. }
  54. }
  55. getShapeArea(obj)
  56. getShapeArea(123)
  57. </script>
  58. </body>
  59. </html>

ES6-JS面向对象的多态理解

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // 多态的表现: JS到处都是多态
  12. function sum(a1, a2) {
  13. return a1 + a2
  14. }
  15. sum(20, 30)
  16. sum("abc", "cba")
  17. // 多态的表现
  18. var foo = 123
  19. foo = "Hello World"
  20. console.log(foo.split())
  21. foo = {
  22. running: function() {}
  23. }
  24. foo.running()
  25. foo = []
  26. console.log(foo.length)
  27. </script>
  28. </body>
  29. </html>

ES6-对象字面量的增强写法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. /*
  12. 1.属性的增强
  13. 2.方法的增强
  14. 3.计算属性名的写法
  15. */
  16. var name = "why"
  17. var age = 18
  18. var key = "address" + " city"
  19. var obj = {
  20. // 1.属性的增强
  21. name,
  22. age,
  23. // 2.方法的增强
  24. running: function() {
  25. console.log(this)
  26. },
  27. swimming() {
  28. console.log(this)
  29. },
  30. eating: () => {
  31. console.log(this)
  32. },
  33. // 3.计算属性名
  34. [key]: "广州"
  35. }
  36. obj.running()
  37. obj.swimming()
  38. obj.eating()
  39. function foo() {
  40. var message = "Hello World"
  41. var info = "my name is why"
  42. return { message, info }
  43. }
  44. var result = foo()
  45. console.log(result.message, result.info)
  46. </script>
  47. </body>
  48. </html>

ES6-数组和对象的解构语法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. var names = ["abc", "cba", undefined, "nba", "mba"]
  12. // 1.数组的解构
  13. // var name1 = names[0]
  14. // var name2 = names[1]
  15. // var name3 = names[2]
  16. // 1.1. 基本使用
  17. // var [name1, name2, name3] = names
  18. // console.log(name1, name2, name3)
  19. // 1.2. 顺序问题: 严格的顺序
  20. // var [name1, , name3] = names
  21. // console.log(name1, name3)
  22. // 1.3. 解构出数组
  23. // var [name1, name2, ...newNames] = names
  24. // console.log(name1, name2, newNames)
  25. // 1.4. 解构的默认值
  26. var [name1, name2, name3 = "default"] = names
  27. console.log(name1, name2, name3)
  28. // 2.对象的解构
  29. var obj = { name: "why", age: 18, height: 1.88 }
  30. // var name = obj.name
  31. // var age = obj.age
  32. // var height = obj.height
  33. // 2.1. 基本使用
  34. // var { name, age, height } = obj
  35. // console.log(name, age, height)
  36. // 2.2. 顺序问题: 对象的解构是没有顺序, 根据key解构
  37. // var { height, name, age } = obj
  38. // console.log(name, age, height)
  39. // 2.3. 对变量进行重命名
  40. // var { height: wHeight, name: wName, age: wAge } = obj
  41. // console.log(wName, wAge, wHeight)
  42. // 2.4. 默认值
  43. var {
  44. height: wHeight,
  45. name: wName,
  46. age: wAge,
  47. address: wAddress = "中国"
  48. } = obj
  49. console.log(wName, wAge, wHeight, wAddress)
  50. // 2.5. 对象的剩余内容
  51. var {
  52. name,
  53. age,
  54. ...newObj
  55. } = obj
  56. console.log(newObj)
  57. // 应用: 在函数中(其他类似的地方)
  58. // function getPosition(position)直接把position解构成{ x, y },方便拿对象里面的参数
  59. function getPosition({ x, y }) {
  60. console.log(x, y)
  61. }
  62. getPosition({ x: 10, y: 20 })
  63. getPosition({ x: 25, y: 35 })
  64. function foo(num) {}
  65. foo(123)
  66. </script>
  67. </body>
  68. </html>

补充-手写apply-call函数实现

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // new Function()
  12. // foo.__proto__ === Function.prototype
  13. function foo(name, age) {
  14. console.log(this, name, age)
  15. }
  16. // foo函数可以通过apply/call
  17. // foo.apply("aaa", ["why", 18])
  18. // foo.call("bbb", "kobe", 30)
  19. // 1.给函数对象添加方法: hyapply
  20. Function.prototype.hyapply = function(thisArg, otherArgs) {
  21. // this -> 调用的函数对象
  22. // thisArg -> 传入的第一个参数, 要绑定的this
  23. // console.log(this) // -> 当前调用的函数对象
  24. // this.apply(thisArg)
  25. thisArg.fn = this
  26. // 1.获取thisArg, 并且确保是一个对象类型
  27. thisArg = (thisArg === null || thisArg === undefined)? window: Object(thisArg)
  28. // thisArg.fn = this
  29. Object.defineProperty(thisArg, "fn", {
  30. enumerable: false,
  31. configurable: true,
  32. value: this
  33. })
  34. thisArg.fn(...otherArgs)
  35. delete thisArg.fn
  36. }
  37. // foo.hyapply({ name: "why" }, ["james", 25])
  38. // foo.hyapply(123, ["why", 18])
  39. // foo.hyapply(null, ["kobe", 30])
  40. // 2.给函数对象添加方法: hycall
  41. Function.prototype.hycall = function(thisArg, ...otherArgs) {
  42. // 1.获取thisArg, 并且确保是一个对象类型
  43. thisArg = (thisArg === null || thisArg === undefined)? window: Object(thisArg)
  44. // thisArg.fn = this
  45. Object.defineProperty(thisArg, "fn", {
  46. enumerable: false,
  47. configurable: true,
  48. value: this
  49. })
  50. thisArg.fn(...otherArgs)
  51. delete thisArg.fn
  52. }
  53. foo.hycall({ name: "why", fn: "abc" }, "james", 25)
  54. foo.hycall(123, "why", 18)
  55. foo.hycall(null, "kobe", 30)
  56. </script>
  57. </body>
  58. </html>

补充-手写apply-call抽取封装

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // new Function()
  12. // foo.__proto__ === Function.prototype
  13. function foo(name, age) {
  14. console.log(this, name, age)
  15. }
  16. // foo函数可以通过apply/call
  17. // foo.apply("aaa", ["why", 18])
  18. // foo.call("bbb", "kobe", 30)
  19. // 1.封装思想
  20. // 1.1.封装到独立的函数中
  21. function execFn(thisArg, otherArgs, fn) {
  22. // 1.获取thisArg, 并且确保是一个对象类型
  23. thisArg = (thisArg === null || thisArg === undefined)? window: Object(thisArg)
  24. // thisArg.fn = this
  25. Object.defineProperty(thisArg, "fn", {
  26. enumerable: false,
  27. configurable: true,
  28. value: fn
  29. })
  30. // 执行代码
  31. thisArg.fn(...otherArgs)
  32. delete thisArg.fn
  33. }
  34. // 1.2. 封装原型中
  35. Function.prototype.hyexec = function(thisArg, otherArgs) {
  36. // 1.获取thisArg, 并且确保是一个对象类型
  37. thisArg = (thisArg === null || thisArg === undefined)? window: Object(thisArg)
  38. // thisArg.fn = this
  39. Object.defineProperty(thisArg, "fn", {
  40. enumerable: false,
  41. configurable: true,
  42. value: this
  43. })
  44. thisArg.fn(...otherArgs)
  45. delete thisArg.fn
  46. }
  47. // 1.给函数对象添加方法: hyapply
  48. Function.prototype.hyapply = function(thisArg, otherArgs) {
  49. this.hyexec(thisArg, otherArgs)
  50. }
  51. // 2.给函数对象添加方法: hycall
  52. Function.prototype.hycall = function(thisArg, ...otherArgs) {
  53. this.hyexec(thisArg, otherArgs)
  54. }
  55. foo.hyapply({ name: "why" }, ["james", 25])
  56. foo.hyapply(123, ["why", 18])
  57. foo.hyapply(null, ["kobe", 30])
  58. foo.hycall({ name: "why" }, "james", 25)
  59. foo.hycall(123, "why", 18)
  60. foo.hycall(null, "kobe", 30)
  61. </script>
  62. </body>
  63. </html>

补充-手写bind函数的实现

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <script>
  11. // apply/call
  12. function foo(name, age, height, address) {
  13. console.log(this, name, age, height, address)
  14. }
  15. // Function.prototype
  16. // var newFoo = foo.bind({ name: "why" }, "why", 18)
  17. // newFoo(1.88)
  18. // 实现hybind函数
  19. Function.prototype.hybind = function(thisArg, ...otherArgs) {
  20. // console.log(this) // -> foo函数对象
  21. thisArg = thisArg === null || thisArg === undefined ? window: Object(thisArg)
  22. Object.defineProperty(thisArg, "fn", {
  23. enumerable: false,
  24. configurable: true,
  25. writable: false,
  26. value: this
  27. })
  28. return (...newArgs) => {
  29. // var allArgs = otherArgs.concat(newArgs)
  30. var allArgs = [...otherArgs, ...newArgs]
  31. thisArg.fn(...allArgs)
  32. }
  33. }
  34. var newFoo = foo.hybind("abc", "kobe", 30)
  35. newFoo(1.88, "广州市")
  36. newFoo(1.88, "广州市")
  37. newFoo(1.88, "广州市")
  38. newFoo(1.88, "广州市")
  39. </script>
  40. </body>
  41. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/78731
推荐阅读
相关标签
  

闽ICP备14008679号