call: 改变了函数运行的作用域,即改变函数里面this的指向apply:同call,apply第二个参数是数组结构例如:
this.name = 'Ab'var obj = {name: 'BBC'}function getName(){ return this.name;}console.log(getName.call(this))console.log(getName.call(obj))console:AbBBC继承功能:
function Person(name, age, sex){ this.name = name this.sex = sex this.age = age this.getAge = function() { return this.age }}
function Student(name, age, sex, grade){ Person.call(this, name,age,sex) //Person.apply(this, arguments) this.grade = grade}var stu = new Student('xiaoming', 23, 'male','one grade')
console.log(stu.name)console.log(stu.age)console.log(stu.sex)console.log(stu.grade)console.log(stu.getAge())
console:
xiaoming
23
male
one grade
23
undefined
Original: https://www.cnblogs.com/mypsq/p/10821032.html
Author: 一样菜
Title: Javascript / Nodejs call 和 apply
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/535830/
转载文章受原作者版权保护。转载请注明原作者出处!