简体   繁体   English

这在 Javascript 变量 function 中叫什么?

[英]What is this called in a Javascript Variable function?

What is the last line of code called?最后一行代码叫什么? I know there is a Javascript variable saveFile being used as a function, passing the parameter "file", but I've never seen the last line of code used before.我知道有一个 Javascript 变量 saveFile 被用作 function,传递参数“file”,但我从未见过之前使用的最后一行代码。

var saveFile = function (file) {

    // do the stuff   

} (typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content);

It's a self-executing anonymous function - with a return value. 这是一个自动执行的匿名 function - 带有返回值。

I know there is a Javascript variable saveFile being used as a function, passing the parameter "file", but I've never seen the last line of code used before.我知道有一个 Javascript 变量 saveFile 被用作 function,传递参数“file”,但我从未见过之前使用的最后一行代码。

This is not what is happening.不是正在发生的事情。 saveFile is not the function - it's a variable that's assigned a value that is returned from that function. saveFile不是function - 它是一个分配了从 function 返回的值的变量。

Here's what the engine does when it runs that code:以下是引擎在运行该代码时所做的事情:

  1. Define the anonymous function.定义匿名 function。
  2. The anonymous function is immediately followed by parenthesis which means the anonymous function should be invoked immediately.匿名 function 后面紧跟括号,这意味着应立即调用匿名 function。
  3. The expression typeof self.== "undefined" && self || typeof window !== "undefined" && window || this.content表达式typeof self.== "undefined" && self || typeof window !== "undefined" && window || this.content typeof self.== "undefined" && self || typeof window !== "undefined" && window || this.content typeof self.== "undefined" && self || typeof window !== "undefined" && window || this.content is evaluated and the resulting value is used as the argument for the file parameter for the anonymous function. typeof self.== "undefined" && self || typeof window !== "undefined" && window || this.content被评估,结果值用作匿名 function 的file参数的参数。
  4. The anonymous function runs its code and returns a value.匿名 function 运行其代码并返回一个值。
  5. That return value is then stored in saveFile .然后将该返回值存储在saveFile中。

It's equivalent to doing this:这相当于这样做:

function anonymous_function( file ) {
    // do the stuff
}

var fileArg = typeof self !== "undefined" && self || typeof window !== "undefined" && window || this.content;

var saveFile = anonymous_function( fileArg );

Let's break this into multiple steps:让我们将其分解为多个步骤:

  1. You are creating a function expression and assigning the value to a variable called saveFile your function defines one parameter file :您正在创建一个function 表达式并将值分配给一个名为saveFile的变量,您的 function 定义了一个参数file

    var saveFile = function (file) {}

  2. Your function gets executed immediately after creation and you pass an argument :您的 function 在创建后立即执行,并且您传递了一个argument

    var saveFile = function (file) { // do the stuff } (typeof self.== "undefined" && self || typeof window;== "undefined" && window || this.content);

  3. Your argument value depends on a short circuit evaluation :您的参数值取决于短路评估

    (typeof self.== "undefined" && self || typeof window;== "undefined" && window || this.content);

Edit: Remember this, functions are first class objects in JavaScript, this simple means that you can do things like passing functions as arguments, declaring a function an assign it to a variable, add functions as values of objects, arrays and even return a function as a value. Edit: Remember this, functions are first class objects in JavaScript, this simple means that you can do things like passing functions as arguments, declaring a function an assign it to a variable, add functions as values of objects, arrays and even return a function作为一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果将被调用方定义为变量,JavaScript函数是否可以知道调用了什么? - Can a JavaScript function know what called it if the callee was defined as a variable? 这个JavaScript函数方法叫什么? - What this JavaScript function method called? 什么是变量等于变量等于 JavaScript 中调用的 function 范例? Vue 示例 - What is the variable equals variable equals function paradigm called in JavaScript? Vue example Javascript中的变量怎么可以是函数,却不能被调用? - How can a variable in Javascript be a function, but not be able to be called? 将Perl变量传递给HTML调用的Javascript函数 - Passing a Perl variable to Javascript function called by HTML 如何将变量传递给另一个要调用的函数 - Javascript - How to pass a variable into another function to be called - Javascript 确定在Chrome中调用什么JavaScript函数 - Determine what javascript function is called in chrome 如何调用这个javascript函数,它使用的是什么模式? - How is this javascript function called, and what pattern is it using? 怎么知道javascript function叫什么? - How to know what javascript function is called? 使用javascript变量更新“ Onlick”中调用的函数的变量 - Update a variable of a function called in “Onlick” using javascript variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM