如何在JavaScript中调用函数调用?

Functions are a way to break down your code into various blocks. o as to write neat, understandable and more modular code. Functional programming is widely used across different platforms with different languages today and so it's important to understand how to deal with functions, more specifically how to invoke or call them? In this article, we'll see how to invoke a function call in JavaScript?

函数是一种将代码分解为各种块的方法。 o编写简洁,可理解且更具模块化的代码。 今天,函数式编程已在具有不同语言的不同平台上广泛使用,因此了解如何处理函数,尤其是如何调用或调用函数很重要。 在本文中,我们将看到如何在JavaScript中调用函数调用?

When you write down a function it simply stays there and does nothing at all. No memory is allotted to its code or it's local variables unless and until you call it.

当您写下一个函数时,它只是停留在那儿,什么也不做。 除非且直到您调用它,否则不会为其代码或其局部变量分配内存。

function multiply(a, b) {
    return a * b;
}

When we type in this, it does nothing. But when we call the multiply() function and pass in some parameters,

当我们键入此内容时,它什么也不做。 但是当我们调用multiple()函数并传递一些参数时,

console.log(multiply(4, 5));

Output

输出量

20

Now it does what it was intended to do. It also allocated some memory for its local variables a and b and also outputted the value it returned to the console.

现在,它可以完成预期的工作。 它还为其局部变量a和b分配了一些内存,并将返回的值输出到控制台。

So is this a pure function? Not associated with any object? Well, JavaScript is all about objects. Everything in JavaScript is essentially an object. Any user-defined functions you make in JavaScript also attach themselves to an object when not stated while declaring and defining them. On the browser, this global object is the window object and you can verify this,

那么这是一个纯函数吗? 没有与任何对象关联? 好吧,JavaScript完全是关于对象的。 JavaScript中的所有内容本质上都是一个对象。 如果在声明和定义它们时未声明,则您在JavaScript中创建的任何用户定义函数也会将自身附加到对象。 在浏览器上,此全局对象是窗口对象,您可以验证此内容,

console.log(window.multiply(4, 5));

Output

输出量

20

We didn't attach the multiply() function as a method to the window object yet we can invoke it as one!

我们没有将multiple()函数作为方法附加到窗口对象,但是我们可以将其作为一个对象调用!

Speaking of objects and global objects, the this keyword attains the value of an object when used inside a function and owns that function. When we call a function without an object and use this inside it, it automatically becomes the global object or attains a global scope.

说到对象和全局对象, this关键字在函数内部使用并拥有该函数时将获得对象的值。 当我们调用没有对象的函数并在其中使用它时,它会自动成为全局对象或达到全局范围。

function greet() {
    return this;
}
console.log(greet());

Output

输出量

Window {parent: Window, postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, …}

Our function returned a global object!

我们的函数返回了一个全局对象!

function greet2() {
    console.log("returning global object inside this!");
    return this;
}
console.log(greet2());

Output

输出量

returning global object inside this!
Window {parent: Window, postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, …}

We can create our objects and use the function as a method. Then use this inside our object to invoke our method call,

我们可以创建对象并将该函数用作方法。 然后在我们的对象中使用它来调用我们的方法调用,

const squirtle= {
    name: 'Squirtle-1',
    HP: 100,
    type: 'water',
    speak: function() {
        return `I'm ${this.name} of ${this.type} and is very healthy! My HP is
 $ {
            this.HP
        }
        `
    }
}

console.log(squirtle.speak());

Output

输出量

"I'm Squirtle-1 of water and is very healthy! My HP is
		100 "

Our squirtle object has a function which is invoked using the object. The object squirtle owns the speak function and we can also verify this,

我们的squirtle对象具有使用该对象调用的功能。 squirtle拥有语音功能,我们也可以对此进行验证,

const squirtle= {
    name: 'Squirtle-1',
    HP: 100,
    type: 'water',
    speak: function() {
        return this;
    }
}

console.log(squirtle.speak());

Output

输出量

{name: "Squirtle-1", HP: 100, type: "water", speak: ƒ}

We can also invoke a function as a constructor using the new keyword,

我们还可以使用new关键字将函数作为构造函数调用

function pokemon(name, type) {
    this.name = name;
    this.type = type;
}

const pikachu = new pokemon('pikachu', 'electric');
console.log(pikachu.name);
console.log(pikachu.type);

Output

输出量

"pikachu"
"Electric"


翻译自: https://www.includehelp.com/code-snippets/how-to-invoke-a-function-call-in-javascript.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值