Rowan Liu's Tech
Home
Blog
CSS
JS
lib
Read
About
GitHub
Home
Blog
CSS
JS
lib
Read
About
GitHub
  • 万丈高楼平地起,地基实在很重要
  • ES2015

    • 概要
    • let和const
    • 函数的扩展
    • Generator异步应用
    • Generator函数
    • Iterator(遍历器)
    • 数值的扩展
    • 对象的扩展
    • Promise
    • /js/ECMAScript6/Proxy-Reflect.html
    • Set/Map
    • 字符串的扩展
    • /js/ECMAScript6/Symbol.html
    • /js/ECMAScript6/variable-destructor-assignment.html
  • basic

    • apply、call
    • ajax
    • arguments
    • 计算盒模型的各种尺寸

apply、call

call方法使用一个指定的this值和单独给出的一个或多个参数来调用一个函数。 apply使用一个指定的this值和一个参数数组或类数组来调用一个函数。

这两个函数的第一个参数都是指定的this值,apply的第二个参数是array,而call的第二个到第N个参数就是第二个到第N个参数。

语法

func.apply(thisArg, [argsArray])

func.apply(thisArg, arg1, arg2, ...)

注意,this值不一定是该函数执行真正的this值,如果函数在非严格模式下,则指定null和undefined的this值会自动指向全局对象(浏览器环境就是window对象),同时值为原始值(数字、字符串、布尔值)的this值会指向该原始值的自动包装对象。

call的用法

1. 调用父构造函数

function Product(name, price){
  this.name = name;
  this.price = price;
}

function Food(name, price){
  Product.call(this, name, price);
  this.category = 'food';
}

var food = new Food('西红柿', 3.5, 'food')

document.write(`名字:${food.name} | 价格: ${food.price} | 类别: ${food.category}`)

参考

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/call

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/apply#Parameters

https://github.com/lin-xin/blog/issues/7

Edit this page
Last Updated: 2026/1/12 10:15
Contributors: Rowan Liu
Next
ajax