[英]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:以下是引擎在运行该代码时所做的事情:
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
参数的参数。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:让我们将其分解为多个步骤:
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) {}
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);
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.