GithubHelp home page GithubHelp logo

javascript-call-apply's Introduction

What is the difference between call and apply?

CALL

Call a function with the specified arguments. You can use call, if you know how many argument are going to pass to the functions.

APPLY

Call a function with argument provided as an array. You can use apply if you don't know how many argument are going to pass to the functions.

Both (call and apply) are using to call a functions.

Here is an advantage over apply and call. The .call() method is little bit faster than .apply() method.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>The Difference Between Call and Apply in JavaScript</title>
    <script>
        var var1 = {myName: "ABC", };

        var var2 = {myName: "XYZ"};

        function preview(par1, par2) {
            alert(this.myName + '-' + par1 + '-' + par2);
            console.log(this.myName, par1, par2);
        }
		
        //using call as given below
        preview.call(var1, "First", "Second"); //output: ABC First Second

        //using apply as given below
        preview.apply(var2, ["First", "Second"]); //output: XYZ First Second

        //using basic as given below
        preview("First", "Second");//output: undefined "First", "Second"
    </script>
</head>
<body>
    <div>
        <h3>Refresh the page and see the alert result or go to console window and see the result.</h3>
    </div>
</body>
</html>

ScreenShot

javascript-call-apply's People

Contributors

sureshalagarsamy avatar

Watchers

 avatar  avatar

Forkers

mumtazahmadui

javascript-call-apply's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.