迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

在 JavaScript 中 apply 与 call 方法

作者:迹忆客 最近更新:2024/03/18 浏览次数:

本文解释了如何在 JavaScript 中使用 call()apply() 方法。使用一些简单的示例解释了这两个函数之间的区别。

让我们看看以下方法及其工作原理。


JavaScript 中的 call() 方法

将所有者对象作为参数是 call() 方法。关键字 this 指的是函数的所有者或它所属的对象。

我们也可以调用一个方法来在不同的对象上使用它。它将让我们使用逗号分隔的参数调用方法。

JavaScript 中 call() 方法的语法

abc_object.object_method.call(object_instance, arguments, ...)

如上所述,call() 方法接受两个参数,我们将在下面描述。

  • 在第一个参数中,object_instance 将持有一个对象的实例。
  • 第二个参数中是逗号分隔的参数。

JavaScript 中的 apply() 方法

apply() 用于在不同的对象上写入。它采用数组形式的值。

JavaScript 中 apply() 方法的语法

abc_object.object_method.apply(object_instance, [arguments_array...])

apply() 方法接受两个参数,如上所示。

  • 在第一个参数中,object_instance 将保存对象的实例。
  • 在第二个参数中,参数是数组的形式。

JavaScript 中的 call() 方法示例

我们将制作一个简单的按钮,点击显示,它将触发点击事件的功能。

该函数将使用 call() 方法,我们将使用包含多个字符串值作为键值对的对象显示组合文本结果。它将通过使用 call() 方法传递参数来简单地组合给定对象的字符串。

结果将显示为一个段落。

<!DOCTYPE html>  
<html>
    <head>
        <title>
call() method Example
</title>
    </head>
    <body style = "text-align:center;">
        <h1 >  
            Click on "Click to display" for display combined Text using call()
        </h1>
        <button onClick="fun()">
          Click to display
        </button>  
        <p id="mypara"></p>  
        <!-- Script tags to use call() method to call
            function -->
        <script>
            function fun() {
              let object_b = {
                combinedText: function(text1, text2) {
                  return this.firstName + " " + this.lastName 
                        + " " + text1 + " " + text2;
                }
              }   
              let object_a = {
                firstName:"DelftStack",
                lastName: "Technology",
              }
              let x = object_b.combinedText.call(object_a, "is", "Best"); 
              document.getElementById("mypara").innerHTML = x;
              }
        </script> 
    </body>
</html>

在上面的 HTML 页面源代码中,你可以看到一个简单的按钮 Click to display,它调用函数 fun()

fun() 主体中使用 object_b 中的另一个 combinedText 方法进行初始化。它返回一个连接的传递参数字符串。

另一个初始化对象 object_a,它包含多个字符串作为键值,使用 call() 参数 (object_a,"is","best")。两者都在字符串中连接并返回结果。

结果由 getElementById() 显示。


使用 apply() 方法的替代方法

apply() 方法可以获得相同的结果。你可以通过传递参数数组而不是逗号分隔的参数 object_b.combinedText.apply(object_a, ["is", "Best"]) 来使用 apply() 方法。

<script>
    function fun() {
        let p = {
        combinedText: function(text1, text2) {
            return this.firstName + " " + this.lastName 
                    + ", " + text1 + ", " + text2;
        }
    }
    let object_a = {
        firstName:"DelftStack",
        lastName: "Technology",
    }
        let x = p.combinedText.apply(object_a, ["Is", "Best"]); 
        document.getElementById("mypara").innerHTML = x;
    }
</script> 

在上面的示例中,x = object_b.combinedText.apply() 方法与参数 (object_a,[array_arguments]) 一起使用。

在下一步中,我们需要在段落 id mypara 中附加结果变量以在 HTML 页面上显示该值。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便